python 多线程

1. 进程

  python /tmp/2.py > /tmp/log&

pid  进程唯一标识符  kill pidNum

创建一个进程的时候,会创建一个主线程

一个进程中只有一个主线程

主线程可以生成多个子线程,主线程和子线程一起就可以称之为多线程

全局锁:  在任意指定的时间里,有且只有一个线程在运行

 1 import time
 2 import threading
 3 
 4 
 5 def test(p):
 6     time.sleep(0.001)
 7     print(p)
 8 
 9 
10 def test2():
11     print('1')
12 
13 
14 ts = []
15 
16 for i in range(0,100):
17     th = threading.Thread(target=test,args=[i])
18     ts.append(th)
19 
20 for i in ts:
21     i.start()
22 
23 for i in ts:
24     i.join()
25 
26 print('hello, end !!'

join 的意思就是会等待所有的子线程都执行完成之后再继续执行(print('hello, end !!)语句)

原文地址:https://www.cnblogs.com/liyugeng/p/7906620.html