How to choose the multi-threaded / multi-process

1. Conclusion:

cpu intensive code (various loop processing, calculation, etc.); a process using a multi-

IO intensive code (document processing, web crawler etc.): Using multithreading

2, explained:

Only one road is single-threaded and single lane only while driving a car;

Multithreading is only one road, but is a multi-lane, multi-vehicles can travel at the same time;

Multi-process there are many roads, each road may be single lane may also be a multi-lane, you can also travel at the same time many cars.

 

Because there GIL, multithreaded python in fact, can only run one thread at a time, just like a driver only while driving a car. Four threads analogous to a four-lane road, but the driver can switch to driving on the car from the car lane B on the driving lane A, the speed of the driver switch fast enough, looks like four on this road vehicles are traveling at the same time. Commander issued orders only need to cross the lane can be transmitted to the driver, the command time transmission loss is relatively small. So for multithreading, we hope that the commander can issue orders more frequently, the driver can quickly get to the command to complete and then switch to the next lane to continue to execute the command, so it looks like the driver while driving four cars . So for IO intensive code, it is recommended to use multiple threads.

For multi-process it, and each road has a driver, four-thread analogous to the 24 Road, the four drivers can drive four cars at the same time. But the commander issued orders need to cross the road in order to pass to the driver, time of command transmission loss is relatively large. So for multi-process, we hope that the commander issued the command after a driver can perform a long time, so you do not have to spend too much time on the transmission of information. So for CPU intensive code, it is recommended to use multiple processes.

Guess you like

Origin www.cnblogs.com/wangyue0925/p/11313421.html