How to perform multitasking:

1. Concurrency

Alternately perform tasks over a period of time. The operating system lets each piece of software execute alternately. For example, the operating system first lets software A run for 0.1 seconds, then quickly switches to software B, lets software B run for 0.1 seconds, and then switches back to A for 0.1 seconds, and the switch continues.

2. Parallel

The operating system assigns different tasks to each CPU, and the cores of multiple CPUs perform multiple tasks at the same time.

How to implement multitasking in Python:

1. Multi-process mode;

Implemented by importing multiprocessing

2. Multi-thread mode;

Implemented by importing threading

3. Multi-process + multi-thread mode;

Implemented by importing multiprocessing and threading

Basic implementation steps of multitasking programming:

1. Import the multiprocessing/threading module

#多进程
import multiprocessing

#多线程
import threading

2. Create a process/thread

# 创建子进程
sub1 = multiprocessing.Process(target=task)

# 创建子线程
sub2 = threading.Thread(target=task)

3. Start a process/thread

#启动子进程
sub1.start()

#启动子线程
sub2.start()

用户bPcYmyV
1 声望0 粉丝