multiple instance on Unix

Jeremy Jones zanesdad at bellsouth.net
Thu Sep 30 17:31:32 CEST 2004


Grant Edwards wrote:

>On 2004-09-30, Nigel King <nigel.king at orthogonsystems.com> wrote:
>  
>
>>On 30 Sep 2004, at 14:11, C Ginger wrote:
>>
>>    
>>
>>>I know the approach to creating a lock file has been around a
>>>long time but there are certain weaknesses to it. There are a
>>>number of race conditions in it. For instance if process A
>>>detects the directory isn't there it will attempt to create
>>>it. During that same time process B might also not find it
>>>there - since A hasn't completed its create yet.
>>>      
>>>
>>This was why I created a directory rather than a file since I
>>thought this was supposed to be atomic.
>>    
>>
>
>AFAIK, creating a file is atomic as well. It's the approach
>that's been used by Unix applications for the past 30 years, so
>if it wasn't atomic, I would think somebody else would have
>noticed the problem and switched to using a lock directory
>before now.
>
>  
>
Right.  But I think the OP's point was that os.mkdir() raises an 
exception if a directory already exists, so you can wrap that in a while 
loop and just keep doing

try:
    os.mkdir('/foo')
except OSError:
    time.sleep(1)
#do stuff
os.rmdir()


and once you get in the #do stuff section, you know you have an 
exclusive lock.  Until this posting, I didn't know that:

os.open('f.txt', os.O_CREAT, os.O_EXCL)

would basically do the same thing.


Jeremy Jones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040930/5cdfdb1f/attachment.html>


More information about the Python-list mailing list