multiple instance on Unix

Jeremy Jones zanesdad at bellsouth.net
Wed Sep 29 13:59:30 EDT 2004


Nigel King wrote:

> Hi,
> I have (my son has!) implemented protection against multiple instances 
> causing havoc by creating a directory. This fails if it exists and 
> thus in a single instruction one gets both the acquire and the test.
>
> Windows has it's mutex which solves the problem. Is there any better 
> version for UNIX.
>
> Thanks
>
> Nigel King
>
Could you explain a little better what you're trying to do?  I'm 
guessing that you (your son - I'm looking forward to that day, myself ) 
have multiple Python processes and you want only one process to be able 
to create a directory?

If so, you could do something like this:

import time
import os
waiting_for_lock = 1

while waiting_for_lock:
    try:
        os.mkdir('/tmp/foo')
        waiting_for_lock = 0
    except OSError:
        print "could not create directory"
        time.sleep(1)
#do whatever you need one and only one process to do...

HTH

Jeremy Jones



More information about the Python-list mailing list