2

I'd like to know how to efficiently check if a variable has had a change in value and if it did, to return this value. For the moment I have something like this:

while(True):
    if paramCHK == x:
        // do this
    elif paramCHK == y:
        // do that
        // and that
        // and that

The problem with above implementation is that when I am in the elif-clause and the parameter changes to x, this is not detected as the execution time of the clause is too long.

What I had in mind is to create a thread and monitor the parameter constantly in a parallel fashion and when a change is detected, report it to the main-function:

myThread():
    if paramCHK.changed():
        notify_main() 

main():
   when notification:
       getParamValue()
       // do something depending the value

How would you solve this in python? Thanks in advance

Kishintai
  • 125
  • 1
  • 1
  • 12
  • 1
    What you want is asynchronous programming / asynchronous method calls in python. I believe this question has already been asked and answered: The question has been thoroughly answered here: https://stackoverflow.com/questions/3221314/asynchronous-programming-in-python and another answer with a working example can be found here: https://stackoverflow.com/questions/1239035/asynchronous-method-call-in-python – clockelliptic Jul 08 '19 at 17:46
  • Thank you, the links are indeed useful – Kishintai Jul 11 '19 at 14:20

1 Answers1

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

This bit of code can go at the start of your while loop, and will detect if x has changed in the last loop, and will run the alert() function if it has

3NiGMa
  • 545
  • 1
  • 9
  • 24