Windows mutex to prevent multiple instances

Frank Buss fb at frank-buss.de
Thu Aug 29 20:26:02 CEST 2002


Hans Nowak <wurmy at earthlink.net> 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 frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de



More information about the Python-list mailing list