How to Use Multi-thread & Multi-process in Python

Download How to Use Multi-thread & Multi-process in Python

Post on 15-Apr-2017

180 views

Category:

Technology

3 download

TRANSCRIPT

www.speedycloud.cn

www.speedycloud.cn20159 WELCOME

How to Use Multi-thread & Multi-process in Python

www.speedycloud.cn

15/8/291

Why using multi-thread?Productivity

www.speedycloud.cn

2

Multi-thread

Multi-processCode Review

www.speedycloud.cn

3

You Got Me!

www.speedycloud.cn

What is GILIn 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-Pythons 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

5

Multi-thread test

www.speedycloud.cn

6

Python multi-thread, no use at all?NO!

www.speedycloud.cn

7

The Good NewsI/O Intensive

www.speedycloud.cn

8

IO Intensive Running Sequence

www.speedycloud.cn

9

UnfortunatelyCPU Intensive Task: Multi-threads productivity even worse than single thread

www.speedycloud.cn

10

How to deal with it?First:Ctypes

www.speedycloud.cn

11

Second:multiprocessing

Extramultiprocessing.dummyHow to deal with it?

www.speedycloud.cn

12

Been there, done that

Join()

www.speedycloud.cn

13

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

Been there, done that

www.speedycloud.cn

14

www.speedycloud.cn

15

Thanks!

Beijing SpeedyCloud Technology Co., Ltd.,www.speedycloud.cn

www.speedycloud.cn