Python: module for creating PID-based lockfile?

Viewed 22639

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) don't step on each others toes. The logical way to do this seems to be a PID-based lockfile… But I don't want to re-invent the wheel if there is already code to do this.

So, is there a Python module out there which will manage the details of a PID-based lockfile?

7 Answers

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.

Related