File lock (flock) compatibility between C and Python
<p>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?</p>

<p><strong>Quick links:</strong></p>

<ol>
<li>Python flock: <a href="https://docs.python.org/2/library/fcntl.html" rel="nofollow">https://docs.python.org/2/library/fcntl.html</a></li>
<li>Linux flock: <a href="http://linux.die.net/man/2/flock" rel="nofollow">http://linux.die.net/man/2/flock</a></li>
</ol>

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

<p>See the <a href="https://hg.python.org/cpython/file/2.7/Modules/fcntlmodule.c#l250" rel="nofollow">source code for the <code>fcntl</code> module</a>:</p>

<pre class="lang-c prettyprint-override"><code>#ifdef HAVE_FLOCK
    Py_BEGIN_ALLOW_THREADS
    ret = flock(fd, code);
    Py_END_ALLOW_THREADS
</code></pre>

<p>This is clearly stated in the <a href="https://docs.python.org/2/library/fcntl.html#fcntl.flock" rel="nofollow"><code>fcntl.flock()</code> documentation</a> as well:</p>

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

<p>So yes, it'll work.</p>

python c linux locking