The difference between multi-threaded and single-threaded applications in Python

First understand the simple operation principle of the CPU:

  It runs very fast. It can run thousands of times in 1s. A core can divide 1s into thousands of time segments. This core can only run one task at the same time; but multiple tasks can be executed alternately. For example, task A can be run in the previous time segment, and task B can be run in the next time segment, and they are executed alternately. Because the time segment is very short, it feels like they are going on at the same time.

  Then understand the difference between single-threaded and multi-threaded:

  Let’s first look at the order process. As the name suggests, it is a process, similar to that on a one-way highway, there is only one lane, and only one vehicle can pass at a time. Multi-process means multiple lanes and can have multiple vehicles at the same time; Strictly speaking, multi-threading is not the understanding of process, because only one thread in a process can run at the same time, and there is no simultaneous operation. The CPU gives us the feeling of simultaneous operation, but it runs very fast, and executes multiple The thread difference may be the difference in milliseconds, microseconds, so you can't feel the difference, they are running at the same time.

  Then there are doubts about single-threading and multi-threading:

  Since the above said, multi-threading is not that multiple threads run concurrently at the same time point, but that the CPU executes each multi-thread alternately in a planned way. What are the advantages of multi-threading? For example, in python, calling the same function multiple times from top to bottom is a single thread, and writing several calling functions into multiple threads, according to the above theory, the CPU running time here does not become faster, because multiple threads cannot run concurrently , it is also completed alternately for each thread class, and even multi-threading may be slower, because it also takes time to manage alternate execution tasks, do not doubt, this is actually the case, so what is the point of using multi-threading?

  Here you need to know about the GIL:

  Python is an interpreted language, so it needs an interpreter when it is running. Briefly describe GIL, that is, global interpreter lock, which means that python will lock the interpreter when it is running, that is, when it is running. It can only be one thread, it is locked and cannot be switched; each thread must apply for the GIL before running, then you must wait for the previous thread to release the lock before you can apply for it, and then execute the code. After execution, You hand it over to the next thread and let it execute the code. The process is as follows:

  Set the GIL  ->  switch to a thread to execute  ->  run  ->  set the thread to sleep  ->  unlock the GIL

  Then repeat the above steps again.

  Multithreading for IO -intensive tasks is much faster than single threading:

  It seems that multi-threading consumes more CPU than single-threading, and the running speed is not faster or even slower. This is relative to computationally intensive tasks ( a lot of calculations are required, which consumes CPU resources, such as calculating pi and making high-definition videos. In terms of decoding, etc., all rely on the computing power of the CPU), such computationally-intensive tasks mainly consume CPU resources, and the multi-threading efficiency of python will not improve, or even slower. See the above GIL for the principle;

  There is also an IO-intensive task. The tasks involving network and disk IO are all IO-intensive tasks. This type of task is characterized by very little CPU consumption, and most of the time of the task is waiting for the IO operation to complete (because of the speed of IO Much lower than the speed of CPU and memory), 99% of the time is spent on IO, and very little time is spent on CPU;

  Forgive me for thinking of crawler unconsciously. Crawler is a typical IO-intensive task. In the case of multi-threaded sending requests: sending a request to receiving the response data from the server depends on the speed of the network, so after sending a request, it is waiting for the server. The GIL lock will be released during the period, and other threads can apply for this lock and send requests. Repeat the above operations until the last request, which is equivalent to the CPU sending multiple requests in a very short period of time. The next step is to wait for the server's response; what if it's single-threaded? After it sends a request, it waits for the server to respond. It will not release the GIL lock until the server has returned data to the client, and then continue to the next request. It can only be queued one by one until the last one is executed. Obviously , the threads here are quite concurrent, much faster than a single thread.

 

  In summary:

  When dealing with computationally intensive tasks, python's multi-threading is inferior to single-threading, and its performance is worse than that of single-threading;

  When dealing with IO -intensive tasks, python's multi-threading is superior to single-threading, and its performance is much better than that of single-threading;

 

Reprinted from: https://www.cnblogs.com/znyyy/p/7999299.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324914804&siteId=291194637