问题
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