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

shindich at my-deja.com shindich at my-deja.com
Tue Oct 24 18:27:35 EDT 2000


In article <39f5e46f_3 at corp.newsfeeds.com>,
  "Joshua Muskovitz" <josh at open.com> wrote:
> 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.
>
> Suggestions gratefully accepted!
>
> -- josh
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
>
Using OS specific features like NT mutex is the easiest way to
accomplish your goal.
The biggest problem here is that different Python processes running on
the same machine are not aware of each other.
One way to solve your problem OS independently is to use a resource
that common to all or most OS as a flag. For instance, you could use a
socket or a file to indicate that your app is running. Both of the
approaches mentioned above have their problems. The file approach
causes problems if you app crashes. In that case there is no telling if
the lock file is present because you app is running or because it
crashed. Sockets are more reliable that way. But remember to set "KEEP
ALIVE" option on your socket, otherwise, depening on your OS'
implementation of sockets, you might never find out that the app
crashed. (This is usually a problem for DOS and Windows 3.x IP stacks.)

Good luck!


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list