I l@ve RuBoard |
6.9 Determining if Another Instance of a Script Is Already Running in WindowsCredit: Bill Bell 6.9.1 ProblemIn a Win32 environment, you want to ensure that only one instance of a script is running at any given time. 6.9.2 SolutionMany tricks can be used to avoid starting multiple copies of an application, but they're all quite fragile�except those based on a mutual-exclusion (mutex) kernel object, such as this one. Mark Hammond's precious win32all package supplies all the needed hooks into the Windows APIs to let us exploit a mutex for this purpose: 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: # Take appropriate action, as this is the second # instance of this script; for example: print 'Oh! dear, I exist already.' exit(1) else: # This is the only instance of the script; let # it do its normal work. For example: from time import sleep for i in range(10): print "I'm running",i sleep(1) print "I'm done" 6.9.3 DiscussionThe string 'A unique mutex name' must be chosen to be unique to this script, but it should not be dynamically generated, as it must be the same for all potential simultaneous instances of the same script. A fresh, globally unique ID generated at script-authoring time would be a good choice. According to the Windows documentation, the string can contain any characters except backslashes (\). On Windows platforms that implement Terminal Services, you can have a prefix of Global\ or Local\, but such prefixes would make the string invalid for Windows NT, 95, 98, and ME. The Win32 API call CreateMutex creates a Windows kernel object of the mutual-exclusion (mutex) kind and returns a handle to it. Note that we do not close this handle; it needs to exist throughout the time this process is running. The Windows kernel takes care of removing the handle (and the object it indicates, if the handle being removed is the only handle to that kernel object) when our process terminates. The only thing we really care about is the return code from the API call, which we obtain by calling the GetLastError API right after it. That code is ERROR_ALREADY_EXISTS if and only if the mutual-exclusion object we tried to create already exists (i.e., if another instance of this script is already running). Note that this approach is perfectly safe and not subject to race conditions and similar anomalies if two instances of the script are trying to start at the same time (a reasonably frequent occurrence, for example, if the user erroneously double-clicks in an Active Desktop setting where a single click already starts the application). The Windows specifications guarantee that only one of the instances will create the mutex, while the other will be informed that the mutex already exists. Mutual exclusion is therefore guaranteed by the Windows kernel itself, and the recipe is entirely solid. 6.9.4 See AlsoDocumentation for the Win32 API in win32all (http://starship.python.net/crew/mhammond/win32/Downloads.html) or ActivePython (http://www.activestate.com/ActivePython/); Windows API documentation available from Microsoft (http://msdn.microsoft.com); Python Programming on Win32, by Mark Hammond and Andy Robinson (O'Reilly, 2000). |
I l@ve RuBoard |