防止多次执行 [英] Preventing multiple executions
问题描述
我有这个Django cron作业脚本(我为此使用kronos,这很棒).
由于我每分钟都会触发一次此作业,因此我想确保没有另一个脚本实例正在运行.如果有先前的作业正在运行,那么我想跳过当前的执行.
我知道我可以使用锁定文件来做到这一点,但这并不是很可靠,当您在执行过程中重新启动(必须清除锁定文件)时,可能会引起问题.
使用Python(在本例中为Django)执行此操作的最佳方法是什么?
编辑:我的目标是Linux,很抱歉忽略了这一点.
此处有一个Django应用: https: //github.com/jsocol/django-cronjobs
也可以在
# myapp/cron.py
import cronjobs
@cronjobs.register
def periodic_task():
pass
点上使用.
以防万一你直接去点子;您可以使用装饰器注册作业,如下所示:
# myapp/cron.py
import cronjobs
@cronjobs.register
def periodic_task():
pass
然后通过以下命令运行命令:
$ ./manage.py cron periodic_task
默认情况下它具有作业锁定,但是您可以在应用装饰器时将其禁用:
@register(lock=False)
I have this Django cron job script (I am using kronos for that, which is great).
Since I trigger this job every minute, I want to make sure that there isn't another instance of the script already running. If there is a previous job running, then I want to skip the current execution.
I know I can do that with a lock file, but that's not very reliable and can cause problems when you reboot in the middle of an execution (you have to clear the lock file), etc.
What's the best way to do this using Python (Django in this case)?
EDIT: I am targeting Linux, sorry for leaving this out.
There's a Django app here: https://github.com/jsocol/django-cronjobs
Also available on pip as cronjobs
.
Just in case you go straight to pip; You register jobs using the decorator like so:
# myapp/cron.py
import cronjobs
@cronjobs.register
def periodic_task():
pass
Then run the command via:
$ ./manage.py cron periodic_task
It has job locking by default but you can disable it when you apply the decorator:
@register(lock=False)
这篇关于防止多次执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!