Python培训——5

10 篇文章 0 订阅
9 篇文章 0 订阅

Python定时器

定义:
# 此类表示一个操作应该在等待一定的时间之后运行 --- 相当于一个定时器。 Timer 类是 Thread 类的子类,因此可以像一个自定义线程一样工作。
用法:
# 与线程一样,通过调用 start() 方法启动定时器。
# 而 cancel() 方法可以停止计时器(在计时结束前), 定时器在执行其操作之前等待的时间间隔可能与用户指定的时间间隔不完全相同。
部分尝试:

timerPrint.py


import time
import threading


def timeCount():
    while 1:
        localtime = time.asctime(time.localtime(time.time()))
        print("时间为 :", localtime)
        time.sleep(1)


def hello():
    timeCount()
    t1 = threading.Timer(2, hello)  # recursive call function -> hello
    t1.start()

main.py

from timerPrint import hello
import threading


def date_p():
    t = threading.Timer(2, hello)
    t.start()  # after 2 seconds, date will be printed


if __name__ == '__main__':
    date_p()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值