如果我有一个全天循环的函数,表示为:
def Show()我如何运行它,使其在一天中的特定时间不会返回任何内容?
在这种情况下,比方说13:50到14:20?
发布于 2021-11-16 20:02:54
如果我没理解错的话,您可以使用if语句检查时间:
# import datetime module
from datetime import datetime as dt
# set to time bounds:
lower_bound = dt.time(13, 50)
upper_bound = dt.time(14, 20)然后只要检查函数每次运行的时间:
now = dt.now().time()
if lower_bound < now < upper_bound:
pass
if not (lower_boun < now < upper_bond):
Show() #that returns something. ...or只需将if语句放入函数本身
查看此question on 和此question on comparing time
答案由chepner和0x5423的有用评论改进
发布于 2021-11-16 20:18:54
schedule是完成所有这些任务的优秀模块。它甚至可以在没有太多工作的情况下异步运行。
https://schedule.readthedocs.io/en/stable/
schedule.every(1).hours.at("06:00").do(show).tag('show', 'guest')然后在特定时间停止调度
schedule.every().day.at("10:30").clear('show')https://stackoverflow.com/questions/69995259
复制相似问题