Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

while data == 1 run action one time

From an api, I receive a data. It returns two values: 1 and -1.
I receive one data per second and in series of 1 or -1.

What I want is that when data goes to 1 you execute the action only once, and when it goes to -1 do the same action always once.

My problem is that for the moment I can’t make the action happen only once. The action is repeated as long as data == 1 or data == -1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Moreover I need the code to continue to execute even if I’m not out of the loop.

Thanks for your help

signal_result = APIDATA

while signal_result == 1:
      self.action(signal_result)
                
while signal_result == -1:
      self.action(signal_result)

>Solution :

Just keep track of the previous value, and if it is different, perform the action:

# at the very start, initialise 
previous_result = 0  # Some value that is different from 1 or -1

# ...
# ...

# When a signal is received:
    global previous_result
    signal_result = APIDATA
    if signal_result != previous_result:
        previous_result = signal_result
        self.action(signal_result)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading