rph
rph

Reputation: 911

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

Upvotes: 5

Views: 1278

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123520

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:

#ifdef HAVE_FLOCK
    Py_BEGIN_ALLOW_THREADS
    ret = flock(fd, code);
    Py_END_ALLOW_THREADS

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.

Upvotes: 5

Related Questions