#导入模块import threading def run(n): print("task",n) #创建线程实例 t1 = threading.Thread(target=run,args=("t1",)) t2 = threading.Thread(target=run,args=("t2",)) #启动线程 t1.start() t2.start()
多线程并行运行的
#启动多个线程实例并行:
import threading import time def run(n): print("task",n) time.sleep(2) for i in range(50): t1 = threading.Thread(target=run,args=("t-%s"%i,)) t2 = threading.Thread(target=run,args=("t2-%s"%i,)) t1.start() t2.start()