By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

help mutex

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.