Welcome, guest | Sign In | My Account | Store | Cart

A script can use this code to determine whether another instance of itself is already in execution. 32-bit MSW only.

Python, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 )

Win32 Programmer's Reference (win32.hlp) The string 'A unique mutex name' must be chosen to be unique to the script. A fresh GUID would be a good choice.