Python锁:我应该将时间成本任务放在锁或条件变量中吗?

时间:2014-09-25 08:06:26

标签: python locking condition-variable

should_go = False
cv = Condition()
while True:
    with cv:
        if not should_go:
            cv.wait()
        if should_go:
            # process_time_cost_tasks()
            should_go = False


def request():
    with cv:
        should_go = True
        cv.notify()

问题:如果我在任务处理期间(很长时间)拥有锁定,这是否重要?

1 个答案:

答案 0 :(得分:0)

您似乎希望将cv用作should_go的互斥锁。 bool assignment is atomic, so this is not needed.

如果您在询问是否阻止process_time_cost_tasks内的锁定会阻止request,它会。这是一个锁定,它是什么。