Allowing only one instance of a script?
Grant Edwards
grante at visi.com
Thu Jun 23 10:58:45 EDT 2005
On 2005-06-23, Thomas Guettler <guettli at thomas-guettler.de> wrote:
> Create a file which contains the PID (process ID) of
> the current process in a directory. If the file
> already exists, the file is running.
That's how it's usually done.
> If your script dies without removing the pid-file, you need to
> look during the start if the PID which is in the file is sill
> alive.
> There is a small race condition between os.path.exists()
> and writing the file.
That's why it's pointless to call os.path.exists().
> If you want to be 100% sure you need to use file locking.
I've never seen it done that way.
The standard method is to use open() with flags O_CREAT|O_EXCL.
If the open() is sucessful, then you have the lock. If it
fails, somebody else already has the lock.
Another method is to create a temp file containing the PID and
then call link() to rename it.
Both open() and link() are atomic operations, so there's no
race condition.
--
Grant Edwards grante Yow! I don't know WHY I
at said that... I think it
visi.com came from the FILLINGS inmy
read molars...
More information about the Python-list
mailing list