from threading import Thread
import time
def func(n): #子线程完成的
time.sleep(1)
print(n)
#多线程示例
for i in range(10):
t = Thread(target=func, args=(i,)) #func的子线程注册到主线程
t.start()
使用面向对象的方式开启新的线程
from threading import Thread
import time
class MyThread(Thread):
def __init__(self,arg):
super().__init__()
self.arg = arg
def run(self):
time.sleep(1)
print(self.arg)
for i in range(10):
t = MyThread(i)
t.start()