I'm puzzled by the behaviour of the lockf function from the python fcntl library: I can't get a shared lock, while the exclusive one works:
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]: 🤔
The error code corresponds to EBADF from the http://man7.org/linux/man-pages/man3/lockf.3.html , which does not make much sense, as f is a writable open file descriptor.
Any ideas?
(Python 3.6.9, Ubuntu 18.04.4 LTS)
fcntl.lockf
sure looks like it should be a wrapper around POSIX lockf
, but it's not. POSIX lockf
doesn't even have shared locks.
fcntl.lockf
is a wrapper around POSIX fcntl
. LOCK_SH
corresponds to F_RDLCK
, which requires a file descriptor opened for reading .
While you're at it, you might want to read about the problems with file locking .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.