How do I force a single instance of a python app?

Darren New dnew at san.rr.com
Fri Oct 27 13:29:48 EDT 2000


Alex Martelli wrote:
> This classic approach is also exposed in Python documentation material,
> such as the talk Guido gave at SD '99 East; see "A simple lock solution":
>     http://www.python.org/doc/essays/ppt/sd99east/sld054.htm

It's not very reliable. I wouldn't want to have (for example) a busy server
using this mechanism, lest processes get locked out indefinitely.  (Of
course, UNIX tends to use the same kind of sloppiness internally, so there's
not much you can improve on it.)

Anyway, another good way of locking is to listen on a particular port. When
your code exits, the listen get closed out, and the new process can start.
If something's already running, you'll get an error you can't rebind. Of
course, one problem is that it may take several seconds between the time the
first proc exits and the new one starts up.

-- 
Darren New / Senior MTS & Free Radical / Invisible Worlds Inc.
San Diego, CA, USA (PST).  Cryptokeys on demand.
The tragedy of the commons applies to monitizing eyeballs, too.



More information about the Python-list mailing list