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

2
  • 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: stackoverflow.com/questions/3221314/… and another answer with a working example can be found here: stackoverflow.com/questions/1239035/… Commented Jul 8, 2019 at 17:46
  • Thank you, the links are indeed useful Commented Jul 11, 2019 at 14:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.