Preventing multiple executions

一世执手 提交于 2019-12-11 12:07:34

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/15561173/preventing-multiple-executions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!