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

2024-05-20 08:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

flock的python实现与标准C库一起透明地工作吗?如果我有两个程序,一个在Python中,另一个在C中,尝试获取一个文件的锁,它能工作吗?在

快速链接:

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

Tags: 文件httpsorg程序httpdocs标准链接
1条回答
网友
1楼 · 发布于 2024-05-20 08:21:04

Python的fcntl库直接构建在标准C库之上;因此在Linux上fcntl.flock()直接使用flockC函数。在

参见source code for the ^{} module

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

这在^{} documentation中也有明确说明:

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().)

所以是的,它会起作用的。在

相关问题 更多 >