Why is it recommended to use multiple processes instead of multithreading in Python? --a colleague's article _php tutorial

Source: Internet
Author: User

Why is it recommended to use multiple processes instead of multithreading in Python? --an article to a colleague


Recently looking at Python multi-threading, often we will hear the veteran said: "Python under the multi-threading is chicken, recommend the use of multi-process!" "But why do you say so?"

To know it, but also to know the reason why. So we have the following in-depth study:

First, the background is emphasized:

1. What is Gil? The full name of the Gil is the global interpreter lock, the source of which is the first consideration of Python design and the decision to make for data security.

2. Each CPU can only execute one thread at a time (multithreading in a single-core CPU is only concurrency, not parallelism, concurrency and parallelism are all the concepts of simultaneous processing of multi-channel requests at the macro level. But concurrency differs from parallelism in that two or more events occur at the same time, while concurrency refers to two or more events that occur within the same interval. )


In Python multithreading, the way each thread executes:
1. Get Gil
2. Execute the code until sleep or a Python virtual machine suspends it.
3. Release the Gil

It can be seen that a thread wants to execute, must first get the Gil, we could think of Gil as a "pass", and in a Python process, the Gil has only one. A thread that does not have a pass will not be allowed to enter CPU execution.


In python2.x, the Gil's release logic is that the current thread meets the IO operation or the ticks count reaches a maximum of (ticks can be seen as a counter of Python itself, specifically for the Gil, which is zeroed after each release, this count can be Sys.setcheckinterval to adjust), to be released.

And each time the Gil Lock is released, the thread is competing for locks and switching threads, consuming resources. And because the Gil Lock exists, a process in Python can always execute only one thread at a time (the thread that gets the Gil can execute), which is why the multithreading efficiency of Python is not high on multicore CPUs.

So is Python's multithreading completely useless?

Here we discuss the following categories:

1, CPU-intensive code (a variety of cycle processing, counting, etc.), in this case, because of the computational work, the ticks count will soon reach the threshold, and then trigger the Gil's release and re-competition (multiple threads switching back and forth is of course the need to consume resources), So multithreading under Python is not friendly to CPU-intensive code.

2, IO-intensive code (file processing, network crawler, etc.), multi-threading can effectively improve efficiency (single-threaded IO operation will be IO wait, resulting in unnecessary time wasted, and open multi-threaded can thread a wait, automatically switch to the threads B, can not waste the resources of the CPU, which can improve program execution efficiency). So Python's multithreading is more friendly to IO-intensive code.


In python3.x, Gil does not use the ticks count, instead uses the timer (the current thread releases the Gil after the execution time reaches the threshold), which is more friendly to the CPU-intensive program but still does not solve the problem that the Gil causes only one thread at a time, so the efficiency is still not satisfactory.


Note: Multi-core multithreading is worse than single-core multithreading, because the single-core multi-threaded, each release Gil, wake up that thread can get to the Gil lock, so it can be executed seamlessly, but Multicore, CPU0 release Gil, other CPU threads will compete, But the Gil may be immediately CPU0, causing several other CPUs to wake up to wait until the switch time and then go to the scheduled state, which will cause a thread bump (thrashing), resulting in less efficiency

Back to the beginning of the question: often we hear veteran say: "Python wants to take full advantage of multi-core CPU, use multi-process", what is the reason?

The reason is that each process has its own Gil, independent of each other, so that it can be executed in real parallel, so in Python, multi-Process execution is more efficient than multi-threading (only for multicore CPUs).


So here's the conclusion: multi-core, want to do parallel lifting efficiency, more common method is to use multi-process, can effectively improve the efficiency of execution

http://www.bkjia.com/PHPjc/1117253.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117253.html techarticle Why is it recommended to use multiple processes instead of multithreading in Python? --a colleague's article recently looking at Python's multi-threaded, often we will hear the veteran say: "Python under the multithreading is chicken ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.