python face questions as many threads okay? Are some allow Python code to run in parallel way

answer

Python does not support multi-threading in the true sense. Python provides multi-threading package , but if you want to improve the speed of the code through multi-threaded, multi-threaded packet is not a good idea. Python has been called a Global Interpreter Lock (GIL) things, it will ensure that any time you multiple threads, only one is executed. Thread execution speed is very fast, and you will mistake threads are executed in parallel, but actually take turns to perform. After this hurdle GIL process, it will increase the cost of execution. This means that if you want to improve code speed, the use of threadingthe package is not a good way.

However, there are a lot of reasons prompted us to use threadingpackage. If you want to perform a number of tasks simultaneously, and without regard to efficiency, the use of this package is not in question, but also very convenient. But in most cases, is not such a thing, you will want to outsource part of the multi-threaded operating system to complete (by opening multiple processes), or some call an external program of your Python code (such as Spark or hadoop), or is it the other code of your Python code calls (for example, you can call C functions in Python, for a large multi-threaded processing overhead work).

Why ask this question

Because GIL is a darn thing (A-hole). Many people spend a lot of time trying to find bottlenecks in your own multithreaded code, until they understand that the presence of the GIL.

This article first appeared in Python black hole net , blog sync with the new park

 

Guess you like

Origin www.cnblogs.com/pythonzhichan/p/11440039.html