You are not logged in.

#1 2023-11-04 13:31:26

Registered: 2013-07-28
Posts: 78

File locking - my recommendations for python

I spent quite a lot of time exploring file locking in python and there are some subtleties that are worth being aware of, so am sharing with the community.

Since python tools are built on the standard C library I started there. Examples are provided:

* C-code sample
    demonstrates standard Posix locking (F_SETFLK) as well as the newer Open File Description version (F_OFD_SETLK)

* Python example : fcntl.fcntl
   I advise against fcntl.fcntl as it requires packing  c data structures, which is brittle.  In fact the python 3.12 doc for packing certainly is not right for arch on x86_64 which has 64 bit elements unlike the 32 bit examples in the python doc.

* Python example : fcntl.flock
    This is my preferred approach using fcntl.flock

Notes and code are available on my tech blog: … le-locking




Board footer

Powered by FluxBB