Get python multithreading in a few minutes

What is multithreading

I won’t say any unfathomable technical terms here. In a word, drink coffee while taking a bath. That is to say, let multiple areas of code or functions run at the same time in the code to achieve the purpose of improving efficiency.

for example

For example, we have such a game, Xiao Ming and Xiao Hong are doing running tests, and now they all have a timer for timing. This is their test one by one. Just like the code below.

import threading
import time
time0=time.time()
def print_ming():
    time.sleep(5)
    print('我是小明我跑完了')

def print_hong():
    time.sleep(3)
    print('我是小红我跑完了')


print_ming()
print_hong()

time1=time.time()
print(time1-time0)

We can see the results
Insert picture description here

Obviously we spent about 8 seconds to finish the test. However, they all have a timer so that putting them together for testing at the same time can greatly improve efficiency.

Simultaneous testing

We typed the code and made the following changes.
Import threading
1 Create thread ming=threading.Thread(target=print_ming)
2 Birth thread ming.setDaemon(True)
3 Run thread The ming.star()
changed code is as follows:



import threading
import time
time0=time.time()
def print_ming():
    time.sleep(5)
    print('我是小明')

def print_hong():
    time.sleep(3)
    print('我是小红')

ming=threading.Thread(target=print_ming)

hong=threading.Thread(target=print_hong)

ming.start()
hong.start()


print('执行完了')
time1=time.time()
print(time1-time0)

The result is as follows:
Insert picture description here
How can shock be so fast, obviously we can find that in this program or test, Xiao Ming and Xiao Hong did not run at all. How could this happen? Obviously, our main program runs faster than the two functions (or sub-functions), so the main program will end after running.

Who runs fast?

Here we obviously need the main program to wait for our two functions. We can assume that there is a coach who runs fast during the running test. As long as one of the three people reaches the end, the test will end. So let's take a look at let the fast runners wait for the slow ones first.
So we added such code on the original basis;
1 Create thread ming=threading.Thread(target=print_ming)
2 Create thread ming.setDaemon(True)
3 Run thread ming.star()
4 Wait thread ming.join ()


import threading
import time
time0=time.time()
def print_ming():
    time.sleep(5)
    print('我是小明')

def print_hong():
    time.sleep(3)
    print('我是小红')

ming=threading.Thread(target=print_ming)

hong=threading.Thread(target=print_hong)

ming.start()
hong.start()
ming.join()

print('执行完了')
time1=time.time()
print(time1-time0)

The result is as follows.
Insert picture description here
Obviously we know that Xiao Ming is afraid of being slow, so we ask everyone to wait for Xiao Ming, but if we change ming.join()
to hong.join(),
Insert picture description here
obviously Xiao Ming has not executed it, so we must use slow as the standard, but Obviously we sometimes don't know who is slower. So just add the two together.
And for the convenience of management, we can add the thread to the loop. The final code is as follows;


import threading
import time
time0=time.time()
def print_ming():
    time.sleep(5)
    print('我是小明')

def print_hong():
    time.sleep(3)
    print('我是小红')
thread=[]
ming=threading.Thread(target=print_ming)
thread.append(ming)
hong=threading.Thread(target=print_hong)
thread.append(hong)
for t in thread:
    t.setDaemon(True)
    t.start()

ming.join()
hong.join()

print('执行完了')
time1=time.time()
print(time1-time0)

Insert picture description here
Note here that I did not add the join() method to the loop, otherwise you will see this result;
Insert picture description here
obviously this has no effect, why is this? You can understand this way, the loop is in the main program, and there are two subroutines in the loop. The program, even if two programs are executed together, must be executed twice each time, which means that the first time Xiaohong runs and the thread ends for 3 seconds, but at this time the main program is not executed and enters the next cycle. At this time, Xiaoming's execution cost 5 A total of 8 seconds.
So we must use join() outside the loop

Final Results


import threading
import time
time0=time.time()
def print_ming():
    time.sleep(3)
    print('我是小明')

def print_hong():
    time.sleep(5)
    print('我是小红')
thread=[]
ming=threading.Thread(target=print_ming)
thread.append(ming)
hong=threading.Thread(target=print_hong)
thread.append(hong)
for t in thread:
    t.setDaemon(True)
    t.start()

ming.join()
hong.join()

print('执行完了')
time1=time.time()
print(time1-time0)
#ming=threading.Thread(target=print_ming,args=(填入函数值,没有值需要被填则不设置args参数))

Insert picture description here

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/105881962