使用 Schedule 在特定时间运行作业 [英] Using Schedule to run jobs at certain times

查看:58
本文介绍了使用 Schedule 在特定时间运行作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安排一些作业,多亏了 stackoverflow,我才能找到 Schedule (https://pypi.python.org/pypi/schedule).

I am trying to schedule a few jobs and thanks to stackoverflow I was able to find Schedule (https://pypi.python.org/pypi/schedule).

不幸的是,这没有被很好地记录下来.我有一个我要运行的作业列表,每个作业在一天中都有特定的时间运行.

Unfortunately this isn't documented very well. I have a list of jobs I am trying to run, each with a certain time in a single day that it is supposed to be run.

for i in jobArray:
    # compareTimes returns a boolean that indicates if it's time to run it, assume it works.
    if compareTimes(i.time):
        # I want to schedule this job for a specific time, i.time
        schedule.every().day.do(job)
while 1:
    schedule.run_pending()
    time.sleep(60)

这是我想出来的.显然这是错误的(它卡在 while 循环中)..run_pending() 没有文档,所以我不知道如何修复它以使其正常工作.请协助.

This is what I came up with. Obviously this is wrong (it gets stuck in the while loop). There is no documentation for .run_pending() so I don't know how to fix it to get it to work. Please assist.

谢谢.

推荐答案

这是我想出来的.显然这是错误的(它卡在while 循环)..run_pending() 没有文档,所以我不知道如何修复它以使其正常工作.请协助.

This is what I came up with. Obviously this is wrong (it gets stuck in the while loop). There is no documentation for .run_pending() so I don't know how to fix it to get it to work. Please assist.

这似乎是正确的行为.调度程序每 60 秒检查一次挂起的作业,并且(我假设)正在它们进来时运行它们.它应该永远运行,因为您是否使用 schedule.every().day.do 永久调度作业(作业).你的问题是你需要在后台运行这个程序.

That appears to be the correct behaviour. The scheduler is checking for pending jobs every 60 seconds, and (I assume) is running them as they come in. It is supposed to run forever, because are you scheduling jobs perpetually with schedule.every().day.do(job). Your problem is that you need to run this program in the background.

如果你对适合生产环境的东西感兴趣,我建议你看看 Celery(我很可能会矫枉过正)或 Python RQ.这些都有很好的文档记录、分发,并且可以开箱即用地在后台运行.

If you are interested in something that is suitable for a production environment, I suggest taking a look at either Celery (which my very well be overkill) or Python RQ. These are well documented, distributed, and can run in the background out of the box.

这篇关于使用 Schedule 在特定时间运行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆