filelock.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:My-Web-Server-Framework-With-Python2.7 作者: syjsu 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号