Python multithreading cannot take advantage of multicore CPUs, but sometimes multithreading is actually faster than a single thread.

Source: Internet
Author: User

Why Python cannot take advantage of multi-core CPUs

Gil is actually because there is a Gil (Global interpreter Lock) in Python, in Chinese: Global interpreter lock.

1, the first time the Gil was designed for data security
Python designed this GIL for data security.
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 occurring at 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

Why is multithreading sometimes less efficient than a single thread?  


1, as above we can know, in Python want a thread to execute must first get
Gil this lock, and Python has only one Gil, to get this Gil to get into CPU execution,
In the Encounter
This lock is released when I/O operation. If the program is purely computational and there is no I/O operation, the interpreter will
100 operations Release this lock, allowing other threads to execute (this number can pass Sys.setcheckinterval
To adjust). So while the CPython line libraries directly encapsulates the native thread of the operating system, CPython
Process as a whole, at the same time there will only be one gain
GIL's thread is running, and the other threads are waiting.
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 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 in multicore
On the CPU, the multithreading efficiency of Python is not high.


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
(various loops, counts, etc.), in which case the ticks count is quickly reached due to the number of computations
100 threshold, then trigger the Gil's release and re-competition (multiple threads switching back and forth is of course required to consume resources), so the multi-threaded multithreading under Python is more efficient than multithreading when it encounters CPU-intensive code.
IO-Intensive code (file processing, web crawler, etc.), multithreading can effectively improve efficiency
(a single-threaded IO operation will wait for an IO, causing unnecessary waste of time, while turning on multithreading can
Thread
A wait, automatically switch to the thread
B, can not waste
CPU resources, which can improve program execution efficiency
)。

Time-sharing can be switched during IO-intensive all this time multithreading faster than a single thread (
5) If Python wants to take full advantage of multi-core CPUs, it can take multiple processes, each with its own
GIL, which does not interfere with each other, so that it can really be executed in parallel, so in
In Python, multi-Process execution is more efficient than multi-threading
(for multi-core CPUs only).
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.

Python multithreading cannot take advantage of multicore CPUs, but sometimes multithreading is actually faster than a single thread.

Related Article

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.