Python learning-entry and threads

Many students have heard that modern operating systems such as Mac OS X, UNIX, Linux, Windows, etc. are all operating systems that support "multitasking".

What is "multitasking"? Simply put, the operating system can run multiple tasks with colleagues. For example, you are using a browser to surf the Internet, while listening to MP3, while using Word to catch up with homework. This is multitasking. At least 3 tasks are running at the same time. There are still many tasks that are quietly running in the background at the same time, but they are not displayed on the desktop.

Nowadays, multi-core CPUs are very popular, but even single-core CPUs in the past can perform multitasking. Because the CPU execution code is executed sequentially, how does a single-core CPU perform multitasking?

The answer is that the operating system takes turns to execute each task alternately. Task 1 executes for 0.01 seconds, then switches to task 2, task 2 executes for 0.01 seconds, then switches to task 3, executes for 0.01 seconds... and so on repeatedly. On the surface, each task is executed alternately, but since the execution speed of the CPU is so fast, we feel as if all tasks are executed at the same time .

The true parallel execution of multitasking can only be achieved on a multi-core CPU. However, because the number of tasks is far more than the number of CPU cores, the operating system will automatically schedule many tasks to each core in turn.

For the operating system, a task is a process (Process) , for example, opening a browser is to start a browser process, opening a notepad starts a notepad process, and opening two notepads starts two notes In this process, opening a Word starts a Word process.

Some processes do more than one thing at the same time , such as Word, which can do things like typing, spell checking, and printing at the same time. In 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" in the process Thread .

Since each process has to do at least one thing, a process has at least one thread . Of course, a complex process like Word can have multiple threads, and multiple threads can be executed at the same time. The execution mode of multiple threads is the same as that of multiple processes. The operating system also switches between multiple threads quickly, so that each The threads all run alternately for short periods of time, seeming to execute simultaneously . Of course, real simultaneous execution of multiple threads requires a multi-core CPU.

All the Python programs we wrote earlier are single-task processes, that is, there is only one thread. What if we want to perform multiple tasks at the same time?

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 a process , so that multiple threads can perform multiple tasks together.

**Of course, there is a third method, which is to start multiple processes, and then start multiple threads for each process.** This way more tasks are executed at the same time. Of course, this model is more complicated and rarely used in practice.

To sum up, there are 3 ways to achieve multitasking:

  • Multi-process mode;
  • Multithreaded mode
  • Multi-process + multi-thread mode;

Inter-process communication:
executing multiple tasks at the same time. Usually, each task is not unrelated, but requires mutual communication and coordination . Sometimes task 1 must be suspended and wait for task 2 to complete before it can continue. Sometimes, task 3 and task 4 They cannot be executed at the same time, so the complexity of multi-process and multi-threaded programs is much higher than the single-process and single-threaded programs we wrote earlier.

Because of the high complexity and difficulty in debugging, it is not a last resort, and we do not want to write multitasking. However, there are many times when it is really impossible without multitasking. Think about watching a movie on a computer. One thread must play the video and the other thread will play the audio. Otherwise, if the single thread is implemented, you can only play the video before playing the audio, or play the audio before playing the video. This obviously won't work.

Python supports both multi-process and multi-threading. We will discuss how to write these two multi-tasking programs.

summary

A thread is the smallest unit of execution, and a process consists of at least one thread . How to schedule processes and threads is completely determined by the operating system, and the program itself cannot decide when to execute, and execute multiple times.

Multi-process and multi-threaded programs involve synchronization and data sharing issues, which are more complicated to write.

Guess you like

Origin blog.csdn.net/qq_44787943/article/details/112586297