Allowing only one instance of a script?

Thomas Guettler guettli at thomas-guettler.de
Thu Jun 23 10:21:55 EDT 2005


Am Wed, 22 Jun 2005 23:49:21 -0700 schrieb Ali:

> Hi,
> 
> I have a script which I double-click to run. If i double-click it
> again, it will launch another instance of the script.
> 
> Is there a way to allow only one instance of a script, so that if
> another instance of the script is launched, it will just return with an
> error.

Hi,

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.

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. If you want to be 100% sure you need
to use file locking.

 HTH,
   Thomas


-- 
Thomas Güttler, http://www.thomas-guettler.de/





More information about the Python-list mailing list