> On 19 Apr 2021, at 22:49, Cameron Simpson <c...@cskk.id.au> wrote:
> 
> On 19Apr2021 23:13, Peter J. Holzer <hjp-pyt...@hjp.at> wrote:
>>> On 2021-04-19 08:54:06 +1000, Cameron Simpson wrote:
>>> My personal preference is lock directories. Shell version goes like
>>> this:
>>> 
>>>    if mkdir /my/lock/directory/name-of-task
>>>    then
>>>       .. do task ..
>>>       rmdir /my/lock/directory/name-of-task
>>>    else
>>>      echo "lock /my/lock/directory/name-of-task already taken"
>>>    fi
>>> 
>>> Simple, reliable, even works over NFS if you care.
>> 
>> Reliable only if "fail locked" is acceptable. If that process dies for
>> some reason the lock directory will stay behind, blocking other
>> processes until somebody notices the problem and removes it.
> 
> A Python context manager narrows the range of circumstances for this 
> failure quite a lot. But yes.
> 
>> The fcntl method suggested by several people has the advantage that the
>> lock vanished with the process which holds it.
> 
> This is very true. OTOH, mkdir's easy to debug if it hangs around.

Only the fcntl method is robust. Your suggestion with mkdir is not
reliable in practice. If you need a lock in the sh env then there are
standard patterns using the flock command. See the man page
for examples.

Barry



> 
> Cheers,
> Cameron Simpson <c...@cskk.id.au>
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to