Windows mutex to prevent multiple instances

Gordon McMillan gmcm at hypernet.com
Thu Aug 29 15:32:16 CEST 2002


[posted and mailed]

Hans Nowak wrote:

> In my current project, we have several programs that should not be run
> multiple times simultaneously. To prevent this from happening, I used a
> mutex

[...]

>              self.mutex = win32event.CreateMutex(None, 1, name)
>              lasterror = win32api.GetLastError()
>              if lasterror == winerror.ERROR_ALREADY_EXISTS:
>                  self.mutex = None
>                  raise SingleInstanceError

[...]

> Now, this works as expected, as long as I try it as the same user.

[...]

> ... Apparently it doesn't work for different
> logins/users/processes or maybe it has something to with permissions. 

The MS Help text says that when using CreateMutex this way (eg, create
or open existing), you should not request ownership in the create call.
So I'd try CreateMutex(None, 0, name), followed by a WaitFor... with
a 0 timeout. When MS words a warning that way (a strong warning with
little or no explanation), I tend to suspect they're hiding a bug...

-- Gordon
http://www.mcmillan-inc.com/




More information about the Python-list mailing list