您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python 2.7中实现超时锁定

如何在Python 2.7中实现超时锁定

详细阐述史蒂文的评论建议:

import threading
import time

lock = threading.Lock()
cond = threading.Condition(threading.Lock())

def waitLock(timeout):
    with cond:
        current_time = start_time = time.time()
        while current_time < start_time + timeout:
            if lock.acquire(False):
                return True
            else:
                cond.wait(timeout - current_time + start_time)
                current_time = time.time()
    return False

注意事项:

python 2022/1/1 18:27:52 有181人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶