def locked_file(fd, no_wait=False):
"""
This function is intended to be used as a ``with`` statement context
manager. It wraps a ``FileLock`` object so that the locking and unlocking
of the file descriptor are automatic. With the ``locked_file()`` function,
you can replace this code:
.. python::
lock = FileLock(fd)
lock.acquire()
try:
do_something()
finally:
lock.release()
with this code:
.. python::
with locked_file(fd):
do_something()
:Parameters:
fd : int
Open file descriptor. The file must be opened for writing
or updating, not reading.
no_wait : bool
If ``False``, then ``locked_file()`` will suspend the calling
process if someone has the file locked. If ``True``, then
``locked_file()`` will raise an ``IOError`` if the file is
locked by someone else.
"""
locked = False
try:
lock = FileLock(fd)
lock.acquire(no_wait)
locked = True
yield lock
finally:
if locked:
lock.release()
filelock.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录