1. 程式人生 > >threading.Timer 延遲執行實例代碼

threading.Timer 延遲執行實例代碼

imp sage 對比 gin asc () work pre form

threading.Timer 實現延遲執行的實例代碼
import time
import threading
import logging

FORMAT = "%(asctime)s %(threadName)s %(thread)d %(message)s"
logging.basicConfig(format=FORMAT, level=logging.INFO)

def worker():
    logging.info(‘in worker‘)
    time.sleep(2)

t = threading.Timer(5, worker)
t.setName(‘w1‘)
# t.cancel()   # 嘗試減少等待時間對比
t.start()
print(threading.enumerate())
# t.cancel()  # 嘗試取消註釋對比
time.sleep(8)  # 嘗試減少等待時間對比
print(threading.enumerate())

threading.Timer 延遲執行實例代碼