help mutex

D

Didier FRAISSE

i want to be sure that only one instance of my script is running at the same
time
i try this little script

#---------------------------------------------------------------------------
# -*- coding: cp1252 -*-

from win32event import CreateMutex
from win32event import ReleaseMutex
from win32api import GetLastError
from winerror import ERROR_ALREADY_EXISTS
from time import sleep

mutex = None

attendre = True
while attendre:
mutex = CreateMutex ( None, 1, 'monmutexbienamoi' )
if (GetLastError ( ) == ERROR_ALREADY_EXISTS):
print 'another instance is running, i'm waiting'
sleep(10)
else:
attendre = False
print 'no instance, i'm running'

for i in range(10000):
print 'i'm working...'

# release the mutex
print 'release mutex %s. another instance could run' %mutex
ReleaseMutex( mutex )
#--------------------------------------------------------------------------

but it doesn't work when i launch multiple instance of my script.
the first one is running without problem.
The other one are waiting and are never released.

where is the trap ??
Didier
 
D

Diez B. Roggisch

but it doesn't work when i launch multiple instance of my script.
the first one is running without problem.
The other one are waiting and are never released.

where is the trap ??

I think you should not try to create, but to actually *lock* the mutex. Then
the other scripts are blocked, until the first one executes. Then the next
one gets access.

I have no w32-api here, but at least thats what I remember from my old days
writing com-objects in C++....

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top