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

Aquarius aquarius at kryogenix.org
Tue Oct 24 20:01:19 EDT 2000


Aquarius spoo'd forth:
> Joshua Muskovitz spoo'd forth:
>> I need to prevent multiple copies of a python app from running
>> simultaneously on a single machine.  In Windows and C++, this is done with
>> sending custom window messages to all top level windows, or using some other
>> kind of scheme.
>> 
>> I'm looking for a platform neutral way to do this for my python apps -- the
>> second instance should somehow detect the first instance and should quietly
>> kill itself.  It does not need to notify the first instance of anything.
>> Initial target platforms are Solaris and NT, but others will follow, so a
>> generic solution would be the best.  File existence is a bad solution
>> because the first instance might (possibly) terminate before it could delete
>> the file.  I need a semaphore which will absolutely go away when the first
>> instance dies, or else a way to reliably probe to see if the first instance
>> exists on the fly.

Another way to do this would be to have instance1 write its PID to the
lockfile. Sadly, I can't find a way of knowing whether a process with a
specific PID is running; you might have to shell out in a
platform-specific way to do this. :-(

You could have the processes signal each other with os.kill() and signal
handlers, but, er, Don't Do That :)

Aq.

-- 
 We learn that the material NSA is withholding from release ...
   describes an NSA database coincidentally called Aquarius.
           -- http://www.cufon.org/cufon/Aquarius/aquarius.htm



More information about the Python-list mailing list