I\'m writing a Python script that may or may not (depending on a bunch of things) run for a long time, and I\'d like to make sure that multiple instances (started via cron)
You can try PID: https://pypi.org/project/pid/
As the documentation shows, you can lock a function simply adding the decorator @pidfile() on the top of function/method name.
from pid.decorator import pidfile
@pidfile()
def main():
pass
if __name__ == "__main__":
main()
The default location for pidfile self check (the file who says if you can execute the code or not) is '/var/run'. You can change it as follows:
@pidfile(piddir='/path/to/a/custom/location')
For other params, see: https://github.com/trbs/pid/blob/95499b30e8ec4a473c0e6b407c03ce644f61c643/pid/base.py#L41
Unfortunatly, this lib's documentation is a little bit poor.