[Python-11] Multithreading and Gil Global lock

Source: Internet
Author: User

1. What is Gil?

The first thing to make clear is that the Gil is not a Python feature, it is a concept introduced when implementing the Python parser (Cpython).

While CPython is the default Python execution environment for most environments, be clear: The Gil is not a Python feature, and Python can be completely independent of the Gil.

2. Why is there a Gil?

In order to make more efficient use of multi-core processor performance, there is a multi-threaded programming, and the resulting is the consistency of data between the threads and the integrity of State synchronization

(For example: Thread 2 requires thread 1 to perform the finished result, but thread 2 is less than thread 1 code, thread 2 executes, thread 1 is still executing, which is the data synchronization)

In order to take advantage of multicore, Python began to support multi-threading, and the simplest way to resolve data integrity and state synchronization between multiple threads was to lock.

3. Gil's Impact

Gil is undoubtedly a global lock, the existence of global lock on multi-threaded efficiency has a small impact. Even Python is almost equal to a single-threaded program.
The following example tests Python single-threaded and multithreaded:

Win7 python3.0+#!_*_coding:utf-8_*_#Author:hkeyImportthreading, timedefrun_thread (): N=0 whileN <= 100000000: N+ = 1defSingle_run (): Start_time=time.time () forIinchRange (4): T= Threading. Thread (target=Run_thread,) T.start () T.join ()#Four threads executed serially    Print('Single thread times:', Time.time ()-start_time)defMulti_run (): Thread_list=[] start_time=time.time () forIinchRange (4): T= Threading. Thread (target=Run_thread,) T.start () thread_list.append (t) forTinchThread_list:t.join ()#Four threads parallel execution    Print('Multi Threads Times:', Time.time ()-start_time)if __name__=='__main__': Single_run () Multi_run ()#The thread's serial and parallel are determined by the join () method, which blocks the current thread and waits for the executing child thread to complete. 

Execution Result:

Single thread times:28.1359999179840129.76200008392334

The results show that the single-threaded serial execution efficiency is faster than multithreading concurrency, which proves the existence of the Gil Global lock

4. Python multithreading parallel execution principle

On a dual-core CPU host, two threads are CPU-intensive, assuming that each thread occupies a single core CPU, because of the Gil Lock,

The same time slice can only have one thread to get the Gil Global lock, and another CPU-consuming thread can not execute, continue to wait, the CPU time is wasted,

That is, only the thread that obtains the Gil lock can actually run on the CPU. Therefore, multithreading can only be executed alternately in Python, even if 100 threads run on a 100-core CPU, only 1 cores are used.

5. How to avoid being affected by the Gil

Since Python's multithreading is so chicken-like on multicore hosts, what better way to implement multiple concurrency?
Using process + coprocessor instead of multithreading
In a multi-process, because each process is independent, each thread within the process has a separate Gil lock that does not affect each other.

However, because of the independent existence of processes, interprocess communication needs to be implemented by means of queues.

[Python-11] Multithreading and Gil Global lock

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.