python - Run while loop until function has returned a value -
i'm trying light 5mm led while function running. when function (more details below) finished , has returned value break while loop.
current code while loop:
pins = [3,5,8,15,16] def piboard(): finished = 0 while finished!=10: pin in pins gpio.output( pin, gpio.high ) time.sleep(0.1) gpio.output( pin, gpio.low ) finished+=1
now in above example run while loop until count equal 10, not best practice. while loop break if next function has returned value.
function want break while loop when returned value
def myfunction(): thread(target = piboard().start() // trying recognize song return song recognized
thanks, - k.
it sounds me want write class extends thread
, implements __enter__
, __exit__
methods make work in with
statement. simple implement, simple syntax, works pretty well. class this:
import threading class blinky(threading.thread): def __init__(self): super().__init__() self.daemon = true self._finished = false def __enter__(self): self.start() def __exit__(self, exc_type, exc_val, exc_tb): self.stop() def run(self): # turn light on while not self._finished: time.sleep(.5) # turn light off def stop(self): self._finished = true
then, run function, put:
with blinky(): my_function()
the light should turn on once with
statement reached , turn off half second after context of with
exited.
Comments
Post a Comment