1. 程式人生 > >python 多線程效果演示

python 多線程效果演示

targe nbsp 結束 class python sleep def start thread

多線程演示

不使用多線程的情況

import threading
import time
def run(n):
    print("task ",n)
    time.sleep(2)

run(‘t1‘) #task  t1  等待2秒
run(‘t2‘) #task  t2 等待2秒程序結束

使用多線程的情況

import threading
import time
def run(n):
    print("task ",n)
    time.sleep(2)
 
t1 = threading.Thread(target=run,args=("t1",))
t2 = threading.Thread(target=run,args=("t2",))
t1.start() #task  t1
t2.start() #task  t2 等待2秒 程序結束

python 多線程效果演示