Single Application Instance Example

Paul McNett p at ulmcnett.com
Tue Jun 14 09:58:27 EDT 2005


Chris Lambacher wrote:
> Does anyone know of an example of how to make a Python program only
> run a single instance.  I am specifically looking for examples for
> win32.

The Python Cookbook, Second Edition, page 380, recipe 9.9: Determining 
Whether Another Instance of a Script is Already Running in Windows, 
suggests using a mutex kernel object to be safe from race conditions:

from win32event import CreateMutex
from win32api import GetLastError
from winerror import ERROR_ALREADY_EXISTS
from sys import exit
handle = CreateMutex(None, 1, 'A unique mutex name')
if GetLastError() == ERROR_ALREADY_EXISTS:
	print "I exist already"
	exit(1)
else:
	print "I don't exist already"
	

> I think I should be able to do this with win32all but the method is
> not obvious.  Preferably I would like to be able to get a handle to
> the already running instance so that I can push it to the foreground
> as well.

There are ways using win32 to get the handle to the existing process, 
but I'm too rusty with win32 to offer that part of the solution.


-- 
Paul McNett
http://paulmcnett.com




More information about the Python-list mailing list