Threads and processes are terms in the operating system. Simply put, each application has its own process.
The operating system will allocate some execution resources to these processes, such as memory space. In the process, some threads can be created, they share these memory spaces and are called by the operating system for parallel computing.
We all know that modern operating systems such as Mac OS X, UNIX, Linux, Windows, etc. can run multiple tasks at the same time. For example, you are surfing the Internet using a browser, typing code, and writing a blog in Markdown. This is multitasking. At least three tasks are running at the same time. Of course, there are many tasks quietly running in the background at the same time, but they are not displayed on the desktop. For the operating system, a task is a process. For example, opening a browser means starting a browser process, opening PyCharm means starting a PtCharm process, and opening Markdown means starting an Md process.
Although multi-core CPUs are now very popular. However, since the CPU execution code is executed sequentially, we will have questions at this time, how does a single-core CPU perform multiple tasks?
In fact, the operating system takes turns to execute each task alternately. Task 1 executes for 0.01 seconds, switches to task 2, task 2 executes for 0.01 seconds, then switches to task 3, executes for 0.01 seconds...and so on. Carry on. On the surface, each task is executed alternately. However, because the execution speed of the CPU is so fast, we cannot recognize it with the naked eye and feel, as if all tasks are executed at the same time.
True parallel execution of multitasking can only be achieved on a multi-core CPU. However, since the number of tasks far exceeds the number of CPU cores, the operating system will automatically schedule many tasks in turn. Execute on each core.
Some processes are not just for one thing, such as browsers. We can play videos, play audios, read articles, edit articles, etc. In fact, these are all in the browser process. subtasks in . Within a process, if you want to do multiple things at the same time, you need to run multiple "subtasks" at the same time. We call these "subtasks" within the process threads.
Since each process has to do at least one thing, a process has at least one thread. Of course, a process can also have multiple threads, and multiple threads can be executed at the same time. The execution method of multi-threading is the same as that of multiple processes. The operating system also quickly switches between multiple threads, allowing each thread to temporarily Run alternately and it will look like they are executed simultaneously.
So what should we do if we want to perform multiple tasks at the same time in Python?
There are two solutions:
One is to start multiple processes. Although each process has only one thread, multiple processes can perform multiple tasks together. .
Another method is to start a process and start multiple threads in one process, so that multiple threads can perform multiple tasks together.
Of course there is a third method, which is to start multiple processes, and each process starts multiple threads, so that more tasks can be executed at the same time. Of course, this model is more complex and is rarely used in practice.
To summarize, there are three ways to implement multitasking:
Multi-process mode;
Multi-thread mode;
Multi-process multi-thread mode.
Execute multiple tasks at the same time. Usually, the tasks are not unrelated, but need to communicate and coordinate with each other. Sometimes, task 1 must pause and wait for task 2 to complete before it can continue to execute. Sometimes, task 3 and Task 4 cannot be executed at the same time, so the complexity of multi-process and multi-thread programs is much higher than the single-process and single-thread program we wrote earlier.
Because of the high complexity and difficulty in debugging, we don’t want to write multitasking unless we have to. However, there are many times when it’s impossible to do without multitasking. Think about watching a movie on a computer. One thread must play the video and another thread plays the audio. Otherwise, if implemented in a single thread, the video must be played first and then the audio, or the audio must be played first and then the video. This is obviously not possible.
Next Section