help mutex

Discussion in 'Python' started by Didier FRAISSE, Oct 23, 2003.

  1. 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
     
    Didier FRAISSE, Oct 23, 2003
    #1
    1. Advertising

  2. > 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
     
    Diez B. Roggisch, Oct 23, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William LaMartin

    What permissions for Mutex to be shared?

    William LaMartin, Oct 16, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    372
    William LaMartin
    Oct 16, 2003
  2. =?iso-8859-1?B?YUKj?=

    Need help on Mutex

    =?iso-8859-1?B?YUKj?=, Mar 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    984
    Scott Allen
    Mar 15, 2005
  3. NaeiKinDus
    Replies:
    1
    Views:
    605
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    640
    James Kanze
    Apr 15, 2007
  5. sven
    Replies:
    2
    Views:
    2,064
    Roy Smith
    Dec 4, 2009
Loading...

Share This Page