how to use multi-thread & multi-process in python

16
北北北北北北北北北北北北 www.speedycloud.cn 北北北北北北北北北北北北 www.speedycloud.cn 2015 北 9 北 北北 WELCOME How to Use Multi-thread & Multi-process in Python

Upload: speedycloud

Post on 15-Apr-2017

207 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: 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

Page 2: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Why using multi-thread?

Productivity

Page 3: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Multi-thread Multi-process

Code Review

Page 4: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

You Got Me!

Page 5: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 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.

Page 6: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Multi-thread test

Page 7: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Python multi-thread, no use at all?

NO!

Page 8: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

The Good News…

I/O Intensive :

Page 9: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

IO Intensive Running Sequence

Page 10: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Unfortunately…CPU Intensive Task: Multi-thread’s

productivity even worse than single thread

Page 11: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

How to deal with it?

First:Ctypes

Page 12: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Second:multiprocessing

Extra :multiprocessing.dummy

How to deal with it?

Page 13: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Been there, done that…Join()

Page 14: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

file discripter

In Linux, all operations on all devices and/or files are done via file descriptor.

Been there, done that…

Page 15: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Page 16: How to Use Multi-thread & Multi-process in Python

北京迅达云成科技有限公司 www.speedycloud.cn

Thanks!

Beijing SpeedyCloud Technology Co., Ltd.,

www.speedycloud.cn