Python | Multithreading for multitasking

Multithreading for multitasking

In real life, there are many scenes where things are happening at the same time, such as dancing and singing at the same time.

In the program, you can use code to simulate the function of singing and dancing:

from time import sleep

def sing():
for i in range(3):

def dance():
for i in range(3):

if __name__ == '__main__':
sing() #sing
dance() #dance
Obviously the program just now did not fulfill the requirement of singing and dancing at the same time
If you want to achieve "singing and dancing" at the same time, then you need a new method called: multitasking
multitasking concept
What is "multitasking"? Simply put, the operating system can run multiple tasks at the same time. For example, you are using a browser to surf the Internet, listening to MP3, and catching up with your homework in Word. This is multitasking, and at least 3 tasks are running at the same time. There are also many tasks quietly running in the background at the same time, just not displayed on the desktop.

Now, multi-core CPUs are very common, but even single-core CPUs of the past can perform multitasking. Since the CPU execution code is executed sequentially, how does a single-core CPU perform multitasking?

The answer is that the operating system alternately executes each task, 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. On the surface, each task is executed alternately, but because the CPU is so fast, we feel as if all tasks are executing at the same time.

True parallel multitasking can only be implemented on multi-core CPUs. However, since the number of tasks is much larger than the number of CPU cores, the operating system will automatically schedule many tasks to be executed on each core in turn.

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

Some processes also do more than one thing at the same time, such as Word, which can do typing, spell checking, printing, etc. 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 as 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 multi-process. The threads all alternate briefly and appear to be executing at the same time. Of course, true simultaneous multi-threading requires multi-core CPUs to be possible.

All the Python programs we wrote earlier are processes that execute a single task, 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 within a process, so that multiple threads can also 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 are performed at the same time. Of course, this model is more complicated and is rarely used in practice.

To sum up, there are three ways to implement multitasking:

multi-process mode;
multithreaded mode;
Multi-process + multi-thread mode.
import threading, time

def dance():
for i in range(50):
print('I'm dancing')

def sing():
for i in range(50):
print('I am singing')

# execute multiple tasks at the same time
# Multitasking in Python: multithreading, multiprocessing, multiprocessing + multithreading

# target needs a function to specify the task that the thread needs to perform
t1 = threading.Thread(target=dance) # created thread 1
t2 = threading.Thread(target=sing) # created thread 2

# start thread

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00