北京迅达云成科技有限公司 www.speedycloud.cn
北京迅达云成科技有限公司
www.speedycloud.cn
2015年9月 北京
WELCOME
浅析python多线程与多进程的使用
北京迅达云成科技有限公司 www.speedycloud.cn
为何要用多线程?
提高效率
北京迅达云成科技有限公司 www.speedycloud.cn
多线程 多进程
Code
Review
北京迅达云成科技有限公司 www.speedycloud.cn
上 当 了!
北京迅达云成科技有限公司 www.speedycloud.cn
什么是GIL?
In C-python, the global interpreter lock, or GIL, is a mutex that
prevents multiple native threads from executing Python byte-codes at
once. This lock is necessary mainly because C-Python’s memory
management is not thread-safe.(However, since the GIL exists, other
features have grown to depend on the guarantees that it enforces.)
GIL将会序列化线程,从而阻止线程并发
北京迅达云成科技有限公司 www.speedycloud.cn
多线程测试
北京迅达云成科技有限公司 www.speedycloud.cn
难道python多线程就没有一点卵用?
非 也
北京迅达云成科技有限公司 www.speedycloud.cn
好消息是
I/O密集型:
北京迅达云成科技有限公司 www.speedycloud.cn
IO 密集型执行时序图
北京迅达云成科技有限公司 www.speedycloud.cn
不幸的是
CPU密集型任务:多线程效率往往比单线程还低
北京迅达云成科技有限公司 www.speedycloud.cn
怎么破?
第一招
Ctypes
北京迅达云成科技有限公司 www.speedycloud.cn
怎么破?
第二招
multiprocessing
号外:multiprocessing.dummy
北京迅达云成科技有限公司 www.speedycloud.cn
踩过的坑
Join()
北京迅达云成科技有限公司 www.speedycloud.cn
踩过的坑
file discripter
在linux内,对所有设备或者文件的操作都是通过文件描述符进行
的
北京迅达云成科技有限公司 www.speedycloud.cn
北京迅达云成科技有限公司 www.speedycloud.cn
Thanks!
北京迅达云成科技有限公司
地址:北京市朝阳区酒仙桥路12号电子城科技大厦1112
网址:www.speedycloud.cn
电话: 400-6911-816

浅析Python多线程与多进程的使用