【发布时间】:2019-07-08 17:07:38
【问题描述】:
我想知道如何有效地检查变量的值是否发生了变化,如果发生了变化,则返回该值。目前我有这样的事情:
while(True):
if paramCHK == x:
// do this
elif paramCHK == y:
// do that
// and that
// and that
上面实现的问题是,当我在elif子句中,参数变为x时,由于子句的执行时间太长,没有检测到。
我的想法是创建一个线程并以并行方式不断监视参数,当检测到更改时,将其报告给主函数:
myThread():
if paramCHK.changed():
notify_main()
main():
when notification:
getParamValue()
// do something depending the value
你会如何在 python 中解决这个问题?提前致谢
【问题讨论】:
-
你想要的是python中的异步编程/异步方法调用。我相信这个问题已经被问到并得到了回答:这个问题已经在这里得到了彻底的回答:stackoverflow.com/questions/3221314/…,另一个带有工作示例的答案可以在这里找到:stackoverflow.com/questions/1239035/…
-
谢谢,链接确实有用
标签: python-3.x multithreading polling