如何使用fcntl读取锁文件状态?
我有一个简单的例子:
#!/usr/bin/python
import time
import fcntl
file = open("e", "w")
fcntl.lockf(file.fileno(),fcntl.LOCK_EX)
file.write("foo")
file.close()
怎么知道一个文件是否被锁定?(不想等到文件解锁后再知道)
1 个回答
5
这就是 fcntl.LOCK_NB
的用途。举个例子:
import warnings
try:
fcntl.flock(myfile, fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError:
warnings.warn("can't immediately write-lock the file ($!), blocking ...")
fcntl.flock(myfile, fcntl.LOCK_EX)
来自 文件访问 的内容。