How to Use Multi-thread & Multi-process in Python
-
Upload
speedycloud -
Category
Technology
-
view
211 -
download
3
Transcript of How to Use Multi-thread & Multi-process in Python
北京迅达云成科技有限公司 www.speedycloud.cn
北京迅达云成科技有限公司 www.speedycloud.cn 2015 年 9 月 北京WELCOME
How to Use Multi-thread & Multi-process in Python
北京迅达云成科技有限公司 www.speedycloud.cn
Why using multi-thread?
Productivity
北京迅达云成科技有限公司 www.speedycloud.cn
Multi-thread Multi-process
Code Review
北京迅达云成科技有限公司 www.speedycloud.cn
You Got Me!
北京迅达云成科技有限公司 www.speedycloud.cn
What is 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 will serialize threads and stop the concurrence of threads.
北京迅达云成科技有限公司 www.speedycloud.cn
Multi-thread test
北京迅达云成科技有限公司 www.speedycloud.cn
Python multi-thread, no use at all?
NO!
北京迅达云成科技有限公司 www.speedycloud.cn
The Good News…
I/O Intensive :
北京迅达云成科技有限公司 www.speedycloud.cn
IO Intensive Running Sequence
北京迅达云成科技有限公司 www.speedycloud.cn
Unfortunately…CPU Intensive Task: Multi-thread’s
productivity even worse than single thread
北京迅达云成科技有限公司 www.speedycloud.cn
How to deal with it?
First:Ctypes
北京迅达云成科技有限公司 www.speedycloud.cn
Second:multiprocessing
Extra :multiprocessing.dummy
How to deal with it?
北京迅达云成科技有限公司 www.speedycloud.cn
Been there, done that…Join()
北京迅达云成科技有限公司 www.speedycloud.cn
file discripter
In Linux, all operations on all devices and/or files are done via file descriptor.
Been there, done that…
北京迅达云成科技有限公司 www.speedycloud.cn
北京迅达云成科技有限公司 www.speedycloud.cn
Thanks!
Beijing SpeedyCloud Technology Co., Ltd.,
www.speedycloud.cn