Detailed explanation of how python supports concurrent methods

Due to the existence of GIL (Global Interpreter Lock), the Python process can only use one core of the CPU at the same time, that is, one of the corresponding operating system
Kernel thread, for a Python web program, if there is a request, and it is a long time-consuming computing task (occupied), this program will receive the first request
Can you handle other requests? If the web program receives the request, it will be while True:

def handle_request(request):while True:
 pass

From the code point of view, Python has only one real thread of execution. When the code goes to while True, it occupies only one CPU core, and it still has a chance to process
Other tasks?

Let's start both threads to perform while True and observe whether they can execute to simulate the two requests:

import time, threading

def f1(name):while True:print(name)
 time.sleep(1)

threading.Thread(target=f1, args=('f1',)).start()
threading.Thread(target=f1, args=('f2',)).start()

Output result:

f1
f2
f2f1
f2
f1

In fact, using Django (a Python Web framework) to test, even if a request executes code like while True, it can still handle other requests (supporting concurrency);

To explain why both while True can be executed:
The GIL lock is still used. The first while True thread can execute this lock before it can execute it. Then it executes a print(name) and then releases the lock.
It pauses, and then the second while True thread gets the GIL and starts to execute, alternately executing around the GIL, thus realizing Python's multithreading.

in conclusion:

While True can't hold CPU resources all the time, it also executes for a while and rests for a while, which gives other processes a chance. There are two key points:

Grab the lock and line up. Arrange a queue for lock, and enter this queue if you want to execute.

The lock release is somewhat similar to process scheduling:

When encountering an IO operation, you need to wait for the IO device to complete the calculation before continuing to execute the thread. During this time, the CPU resources are not occupied, and the lock is released first.
Actively wait, typically sleep, actively give up the lock, wait until a certain time and then re-execute.

The above analysis shows that Python supports concurrency, but due to the inability to take advantage of multi-core processors, for a large number of concurrent computing-intensive applications
Not suitable for using Python.

Recommended Posts

Detailed explanation of how python supports concurrent methods
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
Detailed explanation of Python IO port multiplexing
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
Detailed explanation of the principle of Python super() method
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Detailed explanation of data types based on Python
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Detailed implementation of Python plug-in mechanism
Magic methods and uses of Python
Detailed usage of dictionary in Python
How to verify successful installation of python
Detailed explanation of the use of pip in Python | summary of third-party library installation
Detailed analysis of Python garbage collection mechanism
Python from attribute to property detailed explanation
Detailed usage of Python virtual environment venv
Analysis of common methods of Python multi-process programming
Python drawing | entry-level explanation of weather radar & a variety of radar image visualization methods
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
Analysis of common methods of Python operation Jira library
How about learning python at the age of 27?
Detailed explanation of building Hadoop environment on CentOS 6.5
Detailed examples of using Python to calculate KS
7 features of Python3.9
Detailed explanation of static DNS configuration method in Ubuntu
Detailed explanation of Centos 7 system virtual machine bridging mode
Python drawing | A variety of typhoon path visualization methods
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
Equal Insurance Evaluation: Detailed Explanation of Centos Timeout Exit
Detailed explanation of CentOS7 network setting tutorial in vmware