Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply: GIL blablabla concurrent blablabla
Simply put, it is possible that the only interpreted language that supports multithreading (perl multithreading is disabled, PHP does not have multithreading), and Python multithreading is compromise, only one Python interpreter can interpret Python bytecode at any time.
UPDATE: As commented, Ruby also supports thread, and at least Ruby MRI supports GIL.
If your code is CPU-intensive, multiple threads of code
Very likelyLinear execution. In this case, multithreading is a problem, and the efficiency may be inferior to that of a single thread because of the context switch.
However, if your code is IO-intensive, multithreading can significantly improve efficiency. For example, make a crawler (I don't understand why Python is always linked together with crawlers... But I only remember this example ...), Most of the time crawlers are waiting for the socket to return data. At this time, the C code contains release GIL. The final result is that other threads can continue to execute when a thread waits for IO.
In turn: you should not use Python to write CPU-intensive code... Efficiency...
If you need to use concurrent in CPU-intensive code, use the multiprocessing Library. This library implements the multi thread-like API interface based on multi process and partially implements variable sharing using pickle.
Add another one. if you don't know whether your code is CPU-intensive or IO-intensive, teach you a method:
The multiprocessing module has a dummy sub module, which is an API that implements multiprocessing based on multithread.
Assume that you are using the multiprocessing Pool, which implements concurrency using multiple processes.
from multiprocessing import Pool
Global Interpretor Lock
GIL (Global Interpreter Lock) exists in python's original Interpreter CPython. Therefore, when interpreting and executing python code, A mutex lock is generated to restrict the thread's access to shared resources. GIL is released only when the interpreter encounters an I/O operation or the number of operations reaches a certain number.
Therefore, although the thread library of CPython directly encapsulates the native threads of the system, CPython as a process, only one thread that obtains GIL will be running at the same time, other threads are waiting. This causes multi-thread switching even in multi-core CPUs.
However, the emergence of muiltprocessing can simplify the compilation of multi-process python code to a level similar to multithreading. There is one swimming pool and four pumps, but there is only one person, one person can only open and manage one of them, so the four pumps are useless.
========================================================== ======================================
However, if the pump's working time and cooling recovery time are (thanks to inoahx, it has been changed), the configuration utilization rate is as high as 100%. (This is a metaphor based on personal understanding. if not, add it ). Generally, most of the ideas are due to the existence of GIL. multithreading in Python cannot really use multiple cores and cannot solve the problem of cpu bound, however, some IO bound programs can be well upgraded.
But now we have
CoroutineAh, we can use gevent in the 2.x series. we have asyncio in the standard library of the 3.x series.
. IO bound problems can be completely solved by coroutine. In addition, we can control the scheduling of coroutine independently. Why do we need to use an uncontrollable thread scheduled by the OS?
So I think the thread is a chicken in Python. Especially in the 3.x series.
There are also three technical points (medium) that cannot be introduced in section 2012)
@ Yegle: most of the statements are correct.
Only part of concurrent I/O ...... Why is there a saying that "threads are prepared for programmers who do not understand the state machine? Do I not need to use multi-thread programming for single-core computer programming? This question has already been mentioned.
In the case of concurrent I/O, please make good use of Tornado/Gevent-based libraries. each CPU core starts a process to run an event loop; unless the request processing time is much longer than the I/O and job scheduling time, in this case, you should actually release the task to multiple machines through MQ. Python multithreading is not a weakness. Yes, the GIL stuff is everywhere. even in multi-core Python, it cannot be parallel. well understood, it is equivalent to a time-sharing.
Is there any use of Python multithreading? yes. when you go to the IMG, the process is easily blocked by using a single process and a single thread. multithreading can alleviate this situation. You did not solve the problem. if every request is blocked, multithreading is useless (of course, I have never seen this problem ).
Python is easy to write and use. If you want to perform computation-intensive jobs and want to perform parallel jobs, use C. For computing-intensive tasks, multithreading is not a weakness. it is better to use multiple processes, but for IO-intensive tasks, multithreading is not a weakness because the network IO latency is greater than the CPU. In my opinion, the thread itself is a complicated solution that can be said to be ugly. in 95% of the cases, it is actually not needed-KISS.
If concurrent processing is required, basically non-blocking multiplexing + multi-process methods can be used to handle the vast majority of requirements.