[Tutor] Watch and control access to an executable
andre.roberge at gmail.com
Sat Apr 8 15:33:25 CEST 2006
On 4/8/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > Desktop to the executable). The problem is, only *one* person at a time
> > should run the program.
> The usual way of doing this is simply to create an empty file
> when the program starts and delete it when the program closes
> In python:
Couldn't this approach cause problems if the Python program crashes,
leaving behind the empty file?
However, there is a Cookbook solution that does, I believe, that what
the original poster asked:
It is actually so short that I can even reproduce it below.
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 if this is the second
# instance of this script; for example,
print 'Oh! dear, I exist already.'
exit ( 1 )
A detailed explanation (of a slightly modified version) can be found
in the second edition of the Python Cookbook [Recipe 9.9].
More information about the Tutor