python--Threads and processes

Source: Internet
Author: User

Process: A process is a program that processes can use multi-core CPUs.

Threads: the smallest execution unit in a process, working on a thread

There is at least one thread in a process (master thread) that can have multiple threads (child threads)

Threads are independent of each other. The main thread and child threads are also independent of each other.

The main thread executes the command from beginning to end. Does not wait for a child thread. When the child thread finishes executing, it will disappear.

When multiple threads manipulate one data, it is better to add a lock.

From threading Import Lock

Lock = Lock ()

def run ():

Golbal num=0

Lock.acquire () #加锁

Num+=1

Lock.release () #解锁

Another way to do this: with Lock:

Num+=1

Daemon Thread:

The Guardian thread is the same as the man who was buried with Qin Shihuang

The main thread is Qin Shihuang, and the child thread is the buried man.

Without real concurrency, your computer is a few cores, which can only run a few tasks at the same time.

Multi-Threading inside Python, not using multicore CPUs, can only take advantage of a core CPU

In some cases, multithreading is slower than single thread speed

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. At the very beginning, 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 parallel, concurrency and parallelism are both the concept of processing multiple requests at the same time. But concurrency and parallelism are different, and parallelism means

Two or more events occur at the same time, while concurrency refers to two or more events occurring within the same interval

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 the Gil this lock, and Python only a Gil, get this Gil to get into the CPU execution, in the encounter

This lock is released to the I/O operation. If the program is purely computational and there is no I/O operation, the interpreter releases the lock every 100 times, allowing other threads to execute (this number can

Sys.setcheckinterval to adjust). So while the CPython line libraries directly encapsulates the native thread of the operating system, the CPython process is done as a whole, and at the same time only one gets the

Gil's thread is running, and the other threads are waiting for the Gil to be released.

2, each time the Gil Lock is released, the thread will compete for lock, switch threads, and consume resources. And because the Gil Lock exists, a process in Python can always execute only one thread at a time (get the Gil Line

Process), which is why Python's multithreading efficiency is not high on multicore CPUs.

Why is Python's multithreading not using multicore CPUs, and why is it faster than a single thread?

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 will soon reach a threshold of 100 due to more computational work, then trigger the Gil's release and re-bid

Contention (it is of course necessary to consume resources for multiple threads to switch back and forth), so a single thread is more efficient than multithreading when it encounters CPU-intensive code under Python .

2. IO-Intensive Code (file processing, web crawler, etc.), multithreading can effectively improve efficiency

An IO operation on a single thread will wait for IO, resulting in unnecessary waste of time.

When you turn on multithreading, you can automatically switch to thread B, without wasting 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

If Python wants to take full advantage of multi-core CPUs, it can take multiple processes .

Each process has its own GIL , independent of each other, so that it can be executed in real parallel.

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.

python--Threads and processes

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.