Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何有效地检测Python 3中变量的变化?_Python 3.x_Multithreading_Polling - Fatal编程技术网

Python 3.x 如何有效地检测Python 3中变量的变化?

Python 3.x 如何有效地检测Python 3中变量的变化?,python-3.x,multithreading,polling,Python 3.x,Multithreading,Polling,我想知道如何有效地检查变量的值是否发生了变化,如果发生了变化,则返回该值。目前我有这样的想法: while(True): if paramCHK == x: // do this elif paramCHK == y: // do that // and that // and that 上述实现的问题是,当我在elif子句中并且参数更改为x时,由于子句的执行时间太长,无法检测到这一点 我想到的是创建一个线程,并以

我想知道如何有效地检查变量的值是否发生了变化,如果发生了变化,则返回该值。目前我有这样的想法:

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解决这个问题?提前谢谢

pastx=x
while True:
    if x != pastx:
        pastx=x
        alert()    
    pastx=x

这段代码可以在while循环开始时执行,并将检测x在最后一个循环中是否发生了更改,如果alert()函数有

则将运行该函数,您需要的是python中的异步编程/异步方法调用。我相信这个问题已经被提出并得到了回答:这个问题在这里已经得到了彻底的回答:另一个带有工作示例的答案可以在这里找到:谢谢,链接确实很有用