Python--day40--threading模块
1 import time 2 from threading import Thread 3 4 5 class MyThread(Thread): 6 def __init__(self,arg): 7 super().__init__() 8 self.arg = arg 9 def run(self): 10 time.sleep(1) 11 print(self.arg) 12 13 t = MyThread(10) 14 t.start()
运行结果:
1,一个例子:
1 import os 2 import time 3 from threading import Thread 4 #多线程并发 5 def func(a,b): 6 global g 7 g = 0 8 n = a+b 9 print(n,os.getpid()) 10 11 g = 100 12 t_lst = [] 13 #i的值的范围是0--9 14 for i in range(10): 15 t = Thread(target=func,args=(i,5)) 16 t.start() 17 t_lst.append(t) 18 for t in t_lst:t.join() 19 print(g)
运行结果: