如何在固定时间点将功能安排到 运行?
How to schedule function to run at fix point of time?
是否有任何 python 库可以在固定时间点安排功能(任务)?
例如:
Every 5 mins: runs at xx:00, xx:05, xx:10 ...
Every 30 mins: runs at xx:00, xx:30 ...
Every 1 hours: runs at 00:00, 01:00, 02:00 ...
如果我必须自己实现,有没有方便的工具可以计算我在时间点之前要睡多少秒?
有python-crontab which is useful if your on a linux machine, and in case you are on a windows machine you might want to use the Task Scheduler,
你也可能想使用一些任务运行器,比如 celery.
Python 有一个名为 schedule 的库,它有助于在预定时间安排作业或功能。
使用
安装
pip install schedule
这里是 jupyter notebook python 脚本,它将演示 schedule 的例子
import schedule
import time
def job():
print('I am working as scheduler....!')
def lunch_job():
print("Take lunch break, time is : 2.30 PM")
def screen_break_job():
print("Please take a small break, its been an hour seated")
schedule.every(1).minutes.do(job)
schedule.every().hour.do(screen_break_job)
schedule.every().day.at("14:30").do(lunch_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)
详情请参考schedule
是否有任何 python 库可以在固定时间点安排功能(任务)?
例如:
Every 5 mins: runs at xx:00, xx:05, xx:10 ...
Every 30 mins: runs at xx:00, xx:30 ...
Every 1 hours: runs at 00:00, 01:00, 02:00 ...
如果我必须自己实现,有没有方便的工具可以计算我在时间点之前要睡多少秒?
有python-crontab which is useful if your on a linux machine, and in case you are on a windows machine you might want to use the Task Scheduler, 你也可能想使用一些任务运行器,比如 celery.
Python 有一个名为 schedule 的库,它有助于在预定时间安排作业或功能。
使用
安装pip install schedule
这里是 jupyter notebook python 脚本,它将演示 schedule 的例子
import schedule
import time
def job():
print('I am working as scheduler....!')
def lunch_job():
print("Take lunch break, time is : 2.30 PM")
def screen_break_job():
print("Please take a small break, its been an hour seated")
schedule.every(1).minutes.do(job)
schedule.every().hour.do(screen_break_job)
schedule.every().day.at("14:30").do(lunch_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)
详情请参考schedule