【发布时间】:2020-03-27 09:23:47
【问题描述】:
我对 python fcntl 库中 lockf 函数的行为感到困惑:我无法获得共享锁,而独占锁有效:
In [1]: import fcntl
In [2]: f = open('file', 'w')
In [3]: fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-3-5d23c6a5f968> in <module>
----> 1 fcntl.lockf(f, fcntl.LOCK_SH | fcntl.LOCK_NB)
OSError: [Errno 9] Bad file descriptor
In [4]: fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
In [5]: ????
错误码对应http://man7.org/linux/man-pages/man3/lockf.3.html中的EBADF,没有多大意义,因为f是可写的打开文件描述符。
有什么想法吗?
(Python 3.6.9,Ubuntu 18.04.4 LTS)
【问题讨论】: