Home >Backend Development >Python Tutorial >How to understand processes and threads in python?
In the following article, let’s take a look at what processes and threads in python are. Learn about python processes and threads, and what role python's processes and threads can play in python programming.
What is "multitasking"? Simply put, the operating system can run multiple tasks at the same time. For example, you are surfing the Internet using a browser, listening to MP3 players, and catching up on homework in Word. This is multitasking. At least three tasks are running at the same time. There are many tasks quietly running in the background at the same time, but they are not displayed on the desktop.
Nowadays, multi-core CPUs have become very popular, but even single-core CPUs in the past can perform multiple tasks. Since the CPU execution code is executed sequentially, how does a single-core CPU perform multiple tasks?
The answer is that the operating system takes turns to execute each task alternately. Task 1 executes for 0.01 seconds, switches to task 2, tasks 2 executes for 0.01 seconds, then switches to task 3, executes for 0.01 seconds...and so on. On the surface, each task is executed alternately. However, because the execution speed of the CPU is so fast, we 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 is far greater than the number of CPU cores, the operating system will automatically schedule many tasks to each core in turn. execute on.
For the operating system, a task is a process. For example, opening a browser starts a browser process, opening a Notepad starts a Notepad process, and opening two Notepads starts a process. Two Notepad processes are started, and a Word process is started when a Word is opened.
Some processes can do more than one thing at the same time, such as Word, which can perform typing, spell checking, printing, etc. at the same time. 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 complex process like Word can 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 The threads all alternately run briefly and appear to be executing simultaneously. Of course, truly executing multiple threads simultaneously requires a multi-core CPU to be possible.
All the Python programs we wrote earlier are processes that perform single tasks, 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 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 performed 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:
1. Multi-process mode;
2. Multi-thread mode;
3 .Multi-process and 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 be paused 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.
Python supports both multi-process and multi-threading.
The above is all the content described in this article. This article mainly introduces the relevant knowledge of processes and threads in python. I hope you can use the information to understand the above. content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit thePython tutorial column on the php Chinese website.
The above is the detailed content of How to understand processes and threads in python?. For more information, please follow other related articles on the PHP Chinese website!