详细学一下python中的多线程。
首先需要掌握的是进程和线程的概念,
线程是执行的指令集,是进程的最小执行单位,进程是资源的集合(线程是一个个工人,而进程是一个房子);
线程共享内存空间,进程的内存是独立的(画图软件不能访问QQ)
同一个进程的线程之间可以直接交流,两个进程想通信,必须通过一个中间代理来实现;
创建新线程很简单,创建新进程需要对其父进程进行一次克隆(进程开销大,线程开销小)
一个线程可以控制和操作同一进程里的其他线程,但是进程只能操作子进程;
不能直接比较进程和线程的快慢,因为这两个概念不是一个维度的,进程的启动至少需要一个线程;
python中的多线程
直接看代码
import time import threading start_time = time.time() t_objs = [] def run(n): print('running task ',n ) time.sleep(2) print("task done,",n) for i in range(50): t = threading.Thread(target=run,args=("t-%s"%i ,)) #t.setDaemon(True)#把当前线程变成守护线程 t.start() t_objs.append(t) for t in t_objs: t.join() print("-"*30+"all threads has finished",threading.current_thread(),threading.activeCount()) print("cost:",time.time()-start_time,threading.currentThread())
###
running task t-0
running task t-1
running task t-2
running task t-3
running task t-4
running task t-5
running task t-6
running task t-7
running task t-8
running task t-9
running task t-10
running task t-11
running task t-12
running task t-13
running task t-14
running task t-15
running task t-16
running task t-17
running task t-18
running task t-19
running task t-20
running task t-21
running task t-22
running task t-23
running task t-24
running task t-25
running task t-26
running task t-27
running task t-28
running task t-29
running task t-30
running task t-31
running task t-32
running task t-33
running task t-34
running task t-35
running task t-36
running task t-37
running task t-38
running task t-39
running task t-40
running task t-41
running task t-42
running task t-43
running task t-44
running task t-45
running task t-46
running task t-47
running task t-48
running task t-49
task done, t-1
task done, t-2
task done, t-0
task done, t-4
task done, t-5
task done, t-3
task done, t-6
task done, t-7
task done, t-9
task done, t-8
task done, t-10
task done, t-11
task done, t-12
task done, t-13
task done, t-14
task done, t-16
task done, t-15
task done, t-17
task done, t-19
task done, t-18
task done, t-20
task done, t-22
task done, t-21
task done, t-23
task done, t-24
task done, t-25
task done, t-27
task done, t-26
task done, t-29
task done, t-28
task done, t-30
task done, t-32
task done, t-31
task done, t-33
task done, t-34
task done, t-36
task done, t-35
task done, t-37
task done, t-38
task done, t-40
task done, t-39
task done, t-42
task done, t-41
task done, t-44
task done, t-43
task done, t-47
task done, t-46
task done, t-45
task done, t-49
task done, t-48
------------------------------all threads has finished <_MainThread(MainThread, started 25376)> 1
cost: 2.03932785987854 <_MainThread(MainThread, started 25376)>
每一个run函数都是子线程,t.join是等待每一个子线程执行完毕,接着才会执行之后的主线程(所以采用列表存储线程的方式),也就是两个print函数;如果去掉join,则主线程不会等子线程执行完毕再执行,而是在中间某个时段就执行了;
接着是守护线程的概念
守护线程,顾名思义,是为主线程服务的,程序不会等所有守护线程执行完毕再退出(如上去掉join的时候),而是等主线程执行完毕就立刻退出,效果如下:
import time import threading start_time = time.time() t_objs = [] def run(n): print('running task ',n ) time.sleep(2) print("task done,",n) for i in range(50): t = threading.Thread(target=run,args=("t-%s"%i ,)) t.setDaemon(True)#把当前线程变成守护线程 t.start() t_objs.append(t) # for t in t_objs: # t.join() print("-"*30+"all threads has finished",threading.current_thread(),threading.activeCount()) print("cost:",time.time()-start_time,threading.currentThread())
###
running task t-0
running task t-1
running task t-2
running task t-3
running task t-4
running task t-5
running task t-6
running task t-7
running task t-8
running task t-9
running task t-10
running task t-11
running task t-12
running task t-13
running task t-14
running task t-15
running task t-16
running task t-17
running task t-18
running task t-19
running task t-20
running task t-21
running task t-22
running task t-23
running task t-24
running task t-25
running task t-26
running task t-27
running task t-28
running task t-29
running task t-30
running task t-31
running task t-32
running task t-33
running task t-34
running task t-35
running task t-36
running task t-37
running task t-38
running task t-39
running task t-40
running task t-41
running task t-42
running task t-43
running task t-44
running task t-45
running task t-46
running task t-47
running task t-48
running task t-49
------------------------------all threads has finished <_MainThread(MainThread, started 26016)> 51
cost: 0.035875558853149414 <_MainThread(MainThread, started 26016)>
全局解释器锁GIL
保证同一时刻只有一个线程在执行
Last but not least:
我去NMLGB的中国移动杭研,虽然老子学艺不精,可你那些破烂音响真算不上什么牛逼AI