Python中的线程是使用 threading
模块来实现的。
要创建一个线程,需要从 threading.Thread
类继承,并重写它的 run
方法,在 run
方法中定义线程要执行的操作。
例如:
import threading
class MyThread(threading.Thread):
def run(self):
print("线程开始执行")
thread = MyThread()
thread.start()
Python中的线程是使用 threading
模块来实现的。
要创建一个线程,需要从 threading.Thread
类继承,并重写它的 run
方法,在 run
方法中定义线程要执行的操作。
例如:
import threading
class MyThread(threading.Thread):
def run(self):
print("线程开始执行")
thread = MyThread()
thread.start()