lock.py 文件源码

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

项目:Citation-Fetcher 作者: andrewhead 项目源码 文件源码
def lock_method(lock_filename):
    ''' Use an OS lock such that a method can only be called once at a time. '''

    def decorator(func):

        @functools.wraps(func)
        def lock_and_run_method(*args, **kwargs):

            # Only run this program if it's not already running
            # Snippet based on
            # http://linux.byexamples.com/archives/494/how-can-i-avoid-running-a-python-script-multiple-times-implement-file-locking/
            fp = open(lock_filename, 'w')
            try:
                fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except IOError:
                raise SystemExit(
                    "This program is already running.  Please stop the current process or " +
                    "remove " + lock_filename + " to run this script."
                )

            return func(*args, **kwargs)

        return lock_and_run_method

    return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号