Determine what the calling program is

Cameron Simpson cs at cskk.id.au
Mon Apr 19 17:46:12 EDT 2021


On 19Apr2021 23:13, Peter J. Holzer <hjp-python at 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.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list