Single Instance of app

Tim Hammerquist tim at
Mon Aug 27 02:34:31 CEST 2001

Me parece que Al Gonzalez <alberto at> dijo:
> Platform: Win 2000
> Py Ver: 2.1.1
> Experience: Beginner in Python
> Is there a standard way in Python to determine if an instance on application
> is already running.
> I'd like to switch focus in some utilities rather than run multiple
> instances.
> I had thought about having the app send a request to a server and wait for a
> response if
> none then set itself up as a server in case another version is started.
> Any thoughts would be appreciated.

Assumeing you have write privileges to at least one directory in the
file-system (the app directory or user's home directory are good), you
can perform the same type of process used on Unices.

import os
user_dir = getuserdir()  # dunno how this is done on WinNT/2000
lock_filename = user_dir + 'lock'
if os.path.exists(lock_filename):
    print "process already running"
lock_file = open(lock_filename, 'w')
lock_file.write("%d\n" % os.getpid())
# go on about business as usual

This has worked for decades on *nix. <wink>  If it doesn't work on your
OS, I'd like to know how you end up solving the problem.

It's astonishing how much trouble one can
get oneself into, if one works at it.
    -- Destruction, The Sandman

More information about the Python-list mailing list