[英]How do i run a python function at a certain timestamp time. Without outside software?
我想告诉python脚本在某个时间戳记时间发生时运行某个函数
我已经在寻找运行时间特定的功能,但找不到任何能回答有关计数时间戳的特定问题的东西
#input is number of days till due date
dueDate = int(input('Days until it is due: '))
#86400 seconds in a day
days = dueDate * 86400
#gets current time stamp time
currentT = int(time.time())
#gets the timestamp for due date
alarm = days+currentT
目的是找到在指定的未来时间戳出现时可以从脚本内运行另一个函数的python函数
sched
模块sched
在python中。 这是一篇很好的文章, 这是官方文档。 使用scheduler.enter
可以延迟计划,使用scheduler.enterabs
可以计划特定时间。
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)
def print_event(name):
print('EVENT:', time.time(), name)
now = time.time()
print('START:', now)
scheduler.enterabs(now+2, 2, print_event, ('first',))
scheduler.enterabs(now+5, 1, print_event, ('second',))
scheduler.run()
输出:
START: 1287924871.34
EVENT: 1287924873.34 first
EVENT: 1287924874.34 second
Schedule是一个很好的python模块。
用法:(来自文档)
安装
$ pip install schedule
用法
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
while True:
schedule.run_pending()
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.