python - wait_for 和条件 Python
问题描述
我以某种方式编写了这段代码,一次只允许一个作家,而作家工作时不能有读者。它有效,但我不知道为什么,我知道wait_for()
要醒来有必要打电话notify()
,但我没有而且仍然有效。
谢谢!
import threading
import time
condition = threading.Condition()
def test():
return True
def reader(data):
with condition:
condition.wait_for(test)
print('read')
def writer(data):
with condition:
time.sleep(5)
print('write')
def runThread(data):
if data["w"]: #writer
writer(data)
elif data["r"]: #reader
reader(data)
def run():
#something happens
threading.Thread(target=runThread, daemon=True, ).start()
解决方案
推荐阅读
- amazon-web-services - 代码部署后,来自控制台的更新后的 aws cloudformation 嵌套堆栈自定义参数获取其默认值
- php - 通过 Service-Repository 模式在数据库中创建新记录时如何正确处理异常。[拉拉维尔]
- sql - 存储过程需要参数“':' 附近的语法不正确。”
- ruby - 为什么我不小心在 Ruby 中创建了回文?
- python - Klepto(Python 模块)file_archive 是否应该比 Pickle 慢 10 倍?
- sql-server - 如何将定义的参数添加到 (NEXT VALUE FOR) 命令?
- java - 用Java下载文件时自动跟随重定向?
- c# - 使用 C# 导入 JSON 文件
- c++ - Visual Studio 2019 C++ 跨平台指令 #if #else 被忽略
- python - 点击streamlit按钮后如何保存熊猫数据框?