Inhibiting duplicate apps in Windows?

Cliff Wells logiplexsoftware at earthlink.net
Fri Jun 21 01:09:04 EDT 2002


On Thu, 2002-06-20 at 18:39, Peter Hansen wrote:
> Chris Liechti wrote:
> > 
> > "Edward K. Ream" <edream at tds.net> wrote in news:3D11F9E8.DFFFC8E4 at tds.net:
> > > What I would like is for the second copy of Leo to detect that another
> > > copy of leo.py is already running, send a message to the first copy and
> > > then exit. I wonder if anyone knows how this might be done?
> 
> > dumb solution: create a "lock file" and remove it on programm exit. the
> > other problem is that you have to delete it manually when the app crashes
> > (i deleted may lock files with older Netscapes ;-)
> 
> I wonder whether there's not a simple way to avoid manual deletion.
> If one application creates the lock file, it can hold it open as 
> long as it is running.  A second invocation can detect the first
> by its failure to open the same file for writing.  If the file
> already exists, it can try to delete it with os.remove(). That
> should fail if the first application still has it open, but if 
> the first application actually crashed, the OS should allow the
> file to be removed, at which point the new invocation can safely
> recreate it, knowing it now has the lock.

A fairly fool-proof/portable method is to open a socket. If the app is
run a second time it will fail on the attempt.  Even if the app crashes,
the socket will be closed by the OS (although it may take a couple of
seconds on occasion).

Regards,

Cliff








More information about the Python-list mailing list