The simplest example of Python multithreading

 Look directly at the code

import threading

def say(content):
    print("say " + content)
    

# 当前模块执行时
if __name__ == "__main__":
    threads = []
    for i in range(0, 5):
        name = "thread-" + str(i)
        # 注意这个参数的写法
        t = threading.Thread(target = say, args = (name,))
        threads.append(t)
        t.start()
    
    # 等待所有线程任务结束
    for t in threads:
        t.join()
    
    print("所有线程任务完成")

operation result:

Published 19 original articles · won praise 9 · views 2994

Guess you like

Origin blog.csdn.net/Necrolic/article/details/105564085