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