rkioji rkioji -4 years ago 431
Linux Question

File lock (flock) compatibility between C and Python

Does the python implementation of flock work transparently along with standard C libraries? If I have two programs, one in Python and the other in C, trying acquire a lock on a single file will it work?

Quick links:

  1. Python flock: https://docs.python.org/2/library/fcntl.html

  2. Linux flock: http://linux.die.net/man/2/flock

Answer Source

Python's fcntl library is built directly on top of the standard C libraries; so on Linux fcntl.flock() uses the flock C function directly.

See the source code for the fcntl module:

    ret = flock(fd, code);

This is clearly stated in the fcntl.flock() documentation as well:

fcntl.flock(fd, op)
Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().)

So yes, it'll work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download