Introduction to Python Basics (16): Introduction to Multithreading

Python tutorial video for beginners to watch online for free

https://space.bilibili.com/3461579964156657?spm_id_from=333.1007.0.0

What is multitasking?

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 using Word to catch up with homework. This is multitasking, and at least three tasks are running at the same time. There are still many tasks running quietly in the background at the same time, but they are not displayed on the desktop.

insert image description here

Before understanding the specific implementation of multitasking, let's first understand the concepts of concurrency and parallelism:

Concurrency: Alternately execute multiple tasks over a period of time.

For a single-core cpu to handle multiple tasks, the operating system alternately executes each task in turn. For example: software 1 executes for 0.01 seconds, switches to software 2, software 2 executes for 0.01 seconds, then switches to software 3, and executes for 0.01 seconds... Repeated execution in this way, in fact, each software is executed alternately. However, because the execution speed of the CPU is too fast, on the surface we feel as if these softwares are executing at the same time. Here we need to pay attention to the fact that the single-core CPU executes multiple tasks concurrently.

insert image description here
The most intuitive one is as shown in the figure below:

insert image description here

Parallelism: Executing multiple tasks together, truly simultaneously, over a period of time.

For multi-core CPUs to handle multitasking, the operating system will arrange an execution task for each core of the CPU, and multiple cores are truly executing multiple tasks together at the same time. It should be noted here that the multi-core cpu executes multi-tasks in parallel, and there are always multiple tasks executed together.

insert image description here
In fact, concurrency and parallelism are the specific implementation methods of multitasking: multithreading and multiprocessing

并发可以理解为一件事情由多个人同时去做,相当于我雇佣了很多个工具人帮我抢着做事。对应的在程序中我们可以这么理解:当程序发生阻塞导致程序挂起时,我们可以让程序执行程序后面的任务。需要注意的是程序在同一时间只会执行了个任务。这是多线程的实现原理。

并行可以理解为多个人同时做多件事情。相当于多个人在同一时间做不同的事情,每个人做事都是一个独立的个体。这是多进程的实现原理。

main point

  1. The use of multitasking can make full use of CPU resources, improve the execution efficiency of the program, and make your program have the ability to handle multiple tasks.
  2. There are two ways to execute multitasking:
  • Concurrency: Alternately execute multiple tasks over a period of time.
  • Parallelism: Executing multiple tasks together at the same time for a period of time

Today we learn multi-threaded concurrent programming and apply it to crawler projects

single threaded program

A thread is a single flow of execution. This means that two things will happen to your program at the same time. The different threads don't actually execute concurrently: they just appear to execute concurrently.

Now that you know what a thread is, let's learn how to create one. The Python standard library provides threading. In this module, Thread encapsulates threads well and provides a clean interface to use them.

To start a separate thread, you need to create a Thread instance, then tell it to .start(). Next, let's look at this example from the bubble tea page:

python学习交流Q群:770699889 ###
import time
 
print( '1.洗壶:1min ' )
time.sleep(1)
print( ' 2.灌凉水: 1min ' )
time.sleep(1)
print( '3.烧水: 1min ' )
time.sleep(1)
print('4.等水烧开:3min')
time.sleep(1)
time.sleep(1)
time.sleep(1)
print('5.洗茶杯:1min ' )
time.sleep(1)
print( '6.放茶叶: 1min ' )
time.sleep(1)
print( '7.泡茶: 1min ' )
time.sleep(1)

Guess you like

Origin blog.csdn.net/Dangerous_li/article/details/127623157