day50-线程-定时器

#1、定时器:
from threading import Timer
def func():
    print('定时器')

t = Timer(1,func) #定时一秒,开启func线程。
t.start()

#2、睡眠时间短,线程一直开着,每一秒打印出@@@:
from threading import Thread
import time
def func():
    while True:
        time.sleep(1)
        print('@@@')

t = Thread(target=func)
t.start()

#睡眠时间长,每个小时开启一个线程,每个线程打印完之后结束,再等一个小时又开一个线程执行同样的任务:不浪费cpu资源
from threading import Timer
def func():
    print('@@@')

while True:
    t = Timer(3600,func)
    t.start()
原文地址:https://www.cnblogs.com/python-daxiong/p/12142803.html