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?
This might be of help to you: lockfile
i've been pretty unhappy with all of those, so i wrote this:
to be used something like this:
I know this is an old thread, but I also created a simple lock which only relies on python native libraries:
I believe you will find the necessary information here. The page in question refers to a package for building daemons in python: this process involves creating a PID lockfile.
If you can use GPLv2, Mercurial has a module for that:
http://bitbucket.org/mirror/mercurial/src/tip/mercurial/lock.py
Example usage:
There is a recipe on ActiveState on creating lockfiles.
To generate the filename you can use os.getpid() to get the PID.