Why can't I use multi-core CPUs for multithreading in Python?

Source: Internet
Author: User

Why Python cannot take advantage of multi-core CPUs
GIL:
(1) In fact, because there is a GIL (globalinterpreter lock) in Python, the Chinese is: Global interpreter lock.
1, is the beginning of python for data security design this GIL.
2. Each CPU can only execute one thread at a time:
(Multithreading in a single-core CPU is really just concurrency, not parallelism, concurrency and parallelism are all concepts that simultaneously handle multiple requests at the same time.)
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. )

(2) under Python multithreading, each thread executes in the following manner:
1. Get GIL
2. Execute the code until sleep or a python virtual machine suspends it.
3. Release GIL

(3) Why is multithreaded efficiency sometimes less than a single thread?
1, as above we can know, inPython wants a thread to execute must first getGil this lock, andPython has only oneGil, get this Gil to get into CPU execution,
This lock is released when an I/O operation is encountered. If the program is purely computational and there is no I/O operation, the interpreter will release the lock every 100 times, giving other threads the opportunity
Execution (this number can be adjusted by Sys.setcheckinterval). So while the CPython line libraries directly encapsulates the native thread of the operating system, CPython
Process as a whole, at the same time only one gets the Gil thread running, the other threads are waiting for the Gil's release.
2, each time the Gil Lock is released, the thread will compete for lock, switch threads, and consume resources. And because Gil locks exist,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.

(4)Is the multithreading of Python completely useless? The same code, why sometimes multithreading is slower than single-threaded, and sometimes faster than a single thread?
This is mostly about running code:
1, cpu-intensive code ticks count will soon reach 100 threshold, and then trigger gil release and re-competition
(multiple threads switching back and forth is of course required to consume resources), so python the multithreading encountered cpu-intensive code, Single thread is more efficient than multithreading.  
2, IO-Intensive code (single-threaded io operation will be io wait, resulting in unnecessary time wasted, and turn on multithreading can be
Thread a wait, automatically switch to thread b, can not waste cpu resources, which can improve program execution efficiency ). io intensive time-sharing switch all this time multithreading faster than single thread

(5) If Python wants to take full advantage of multicore CPUs, it can use multiple processes,
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 ).
Therefore, in the multi-core CPU, want to do parallel boost efficiency, the more common method is to use multi-process, can effectively improve the efficiency of execution.




Why can't I use multi-core CPUs for multithreading in Python?

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.