Recipe 9.9. Determining Whether Another Instanceof a Script Is Already Running in Windows
Credit: Bill Bell
Problem
In a Windows environment, you
want to ensure that only one instance of a script is running at any
given time.
Solution
Many tricks can be used to avoid starting multiple copies of an
application, but they're all quite
fragileexcept those based on a mutual-exclusion
(mutex) kernel object, such as this one. Mark
Hammond's precious PyWin32
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)
Discussion
The string 'A unique mutex name' must be chosen to
be unique to this script, and it must not be dynamically generated,
because the string must have the same value for all potential
simultaneous instances of the same script. A fresh, globally unique
ID that you manually generate and insert 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 optionally prefix the string with
Global\ or Local\, but such
prefixes would make the string invalid for most versions of Windows,
including 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, because it needs to exist throughout the time this
process is running. It's important to let the
Windows kernel take 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).This approach is perfectly safe and not subject to race conditions
and similar anomalies, even if two instances of the script are trying
to start at the same time (a reasonably frequent occurrence, e.g., 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 as solid as the operating system.
See Also
Documentation for the Win32 API in PyWin32
(http://starship.python.net/crew/mhammond/win32/Downloadsl)
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).