Playing with the history of python(1) multithreading 1

At the end of 2017, I left my old club. After leaving the company, I started a business with a small partner and was responsible for determining the technology stack and the development and deployment of the server. After some comprehensive consideration, I decided to use python as our backend language. Although I only have one and a half years of work experience, I am still a rookie, but I also understand that an excellent programmer should not only satisfy the realization of functions, but also have a relentless pursuit of performance. The multithreading mentioned here is an improvement means of performance.

However, multithreading in python is different from multithreading in other languages. My last job was java development. After looking up some python materials these days, I think there is a huge difference between python's multithreading and java's multithreading. The same problem, the solution of java may not be applicable to python. Therefore, in-depth study of the python language is necessary.

No matter which language you use for multithreaded programming, you must first understand the concept of threads. The definition of thread will not be repeated here, there are many online, but remember that thread is a concept at the operating system level. What would it be like to run a program directly on a computer without using an operating system? Let me explain first, I haven't done this before :P, because it's very troublesome to do this, and friends who play embedded or need to deal with the underlying firmware often should have a deep understanding. The general process is as follows:

  1. The binary machine code of the program is loaded into the memory, the registers of the CPU are initialized, and the instruction pointer register points to the first instruction (this register holds the address of the instruction in the memory);
  2. The CPU fetches the instruction from the memory through the circuit according to the address stored in the instruction pointer register, and then executes it (after the instruction is fetched, the instruction pointer register will auto-increment, and the fetched instruction needs to be decoded, which involves the knowledge of instruction length, instruction pipeline, etc. , it is inconvenient to talk about it, if you are interested, you can check the information yourself, and recommend Wang Shuang's "Assembly Language");
  3. Repeat step 2 until the program ends.

In this process, all registers in the CPU only provide services for the currently executing program. If the execution of this program is interrupted in the middle, and the CPU executes other programs, what should I do now if I want to continue the execution of this poor program instead of re-executing it? In fact, as long as the CPU executes other programs, all the values ​​in the registers are saved. After the CPU finishes executing other programs, restore the pre-saved register values ​​to their original positions.

The clock frequency of the CPU is now getting faster and faster. Write a program that accumulates 10,000 times in python. After running, it gives us the feeling that it can give an answer in an instant. There are also various IO devices that are also getting faster, but compared to the clock frequency of the CPU, the IO speed is still very slow. Therefore, when the IO operation is performed, the CPU is idle. At this time, the CPU is handed over to other programs that need to operate, which will improve the utilization of the CPU. But for programs that use the CPU most of the time, it is better not to use this operation, because saving the register value and then reloading it is also time-consuming (relative to CPU instruction cycles).

In fact, the above is some low-level details of multi-threading, and the operation of saving the value of the register and then reloading it is very close to the context switch we often mention (the specific implementation is slightly different). The CPU-intensive and IO-intensive programs are also mentioned above. You can check and compare the instruction cycle of the CPU, the time required to read and write 1 byte of the disk, and the time required to read and write 1 byte of the network card, which will affect the use of multi-threading. The importance of having an intuitive feel.

Guess you like

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