Preface
Overview of processes and Threads:
Many students have heard that modern operating systems, such as Mac OS X,unix,linux,windows, are supported by "multitasking" operating systems.
What do you mean "multitasking"? To put it simply, the operating system can run multiple tasks at the same time. For example, while you're surfing the Internet with your browser and listening to MP3, while you're working with Word, that's multitasking, at least 3 tasks running at the same time. There are a lot of tasks quietly running in the background at the same time, but the desktop is not displayed.
Multicore CPUs are now very popular, but even the single-core CPUsof the past can do multitasking. Since the CPU execution code is executed sequentially, how does a single-core CPU perform multi-tasking?
The answer is that the operating system turns each task to perform alternately , Task 1 executes 0.01 seconds, switches to Task 2, Task 2 executes 0.01 seconds, then switches to Task 3, executes 0.01 seconds ... This is done repeatedly. On the surface, each task is executed alternately, but because the CPU is executing too fast, we feel as if all the tasks are executing at the same time.
true parallel multitasking can only be done on multicore CPUs , but because the number of tasks is much larger than the number of cores in the CPU, the operating system automatically shifts many tasks to each core.
For the operating system, a task is a process, such as open a browser is to start a browser process, open a notepad started a Notepad process, open two Notepad started the two Notepad process, open a word started a word process.
Some processes do more than one thing at the same time, such as word, which can be typed, spell-checked, and printed at the same time. Within a process , to do multiple tasks at the same time, you need to run multiple "subtasks" at the same time, and we refer to these "subtasks" in the process as threads (thread).
Because each process has at least one thing to do, a process has at least a single thread . Of course, a complex process such as word can have multiple threads, multiple threads can execute simultaneously, multithreading is performed the same way as multiple processes, and the operating system quickly switches between multiple threads , allowing each thread to run momentarily alternately. It looks like it's done at the same time. Of course, a multi-core CPU is required to actually execute multiple threads at the same time.
All of the Python programs we wrote earlier are those that perform single-task processes, that is, only one thread. What if we want to do multiple tasks at the same time?
There are two types of solutions:
One is to start multiple processes, although each process has only one thread, but multiple processes can perform multiple tasks in one piece.
Another way is to start a process that starts multiple threads within a process, so that multiple threads can perform multiple tasks in one piece.
Of course, there is a third way, that is, to start multiple processes, each process to start more than one thread, so that the simultaneous execution of more tasks, of course, this model is more complex, and rarely used.
To summarize, there are 3 ways to implement a multitasking:
-
- Multi-process mode;
- multithreaded mode;
- Multi-process + multithreaded mode.
Python Development notes: Processes