简体   繁体   English

C和Python之间的文件锁(flock)兼容性

[英]File lock (flock) compatibility between C and Python

Does the python implementation of flock work transparently along with standard C libraries? flock的python实现是否与标准C库一起透明地工作? If I have two programs, one in Python and the other in C, trying acquire a lock on a single file will it work? 如果我有两个程序,一个在Python中,另一个在C中,尝试获取单个文件的锁定它会起作用吗?

Quick links: 快速链接:

  1. Python flock: https://docs.python.org/2/library/fcntl.html Python flock: https//docs.python.org/2/library/fcntl.html
  2. Linux flock: http://linux.die.net/man/2/flock Linux flock: http//linux.die.net/man/2/flock

Python's fcntl library is built directly on top of the standard C libraries; Python的fcntl库直接构建在标准C库之上; so on Linux fcntl.flock() uses the flock C function directly . 所以在Linux上, fcntl.flock() 直接使用flock C函数。

See the source code for the fcntl module : 请参阅fcntl模块源代码

#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()文档中也有明确说明:

fcntl.flock(fd, op)
Perform the lock operation op on file descriptor fd (file objects providing a fileno() method are accepted as well). 对文件描述符fd执行锁定操作op fileno()也接受提供fileno()方法的文件对象)。 See the Unix manual flock(2) for details. 有关详细信息,请参阅Unix手册flock(2) (On some systems, this function is emulated using fcntl() .) (在某些系统上,使用fcntl()模拟此函数。)

So yes, it'll work. 是的,它会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM