Windows mutex to prevent multiple instances

Frank Buss fb at
Thu Aug 29 14:26:02 EDT 2002

Hans Nowak <wurmy at> 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, roughly like this (some irrelevant lines snipped):

Your solution is not platform independent. Perhaps you can use a socket, 
which should work on all platforms:

import socket
isRunningSocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: isRunningSocket.bind(('', 12345))
except socket.error: raise SingleInstanceError

Choose a port number you don't use instead of 12345.

I'm not sure, but I think the isRunningSocket variable must be visible in 
the global context, because I've testet it with a local variable and the 
socket was closed after leaving the function.

Frank Buß, fb at,

More information about the Python-list mailing list