Single Instance of app

Peter Hansen peter at engcorp.com
Sun Aug 26 23:02:53 EDT 2001


Tim Hammerquist wrote:
> 
> 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"
>     sys.exit(1)
> lock_file = open(lock_filename, 'w')
> lock_file.write("%d\n" % os.getpid())
> lock_file.close()
> # 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.

Are you certain *that* particular pattern has worked for decades?
It looks like it has a classic synchronization problem, when 
two processes are started simultaneously and they both make it
past the os.path.exists() line before either executes the open()
statement.  Or is this just a risk people live with?

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list