Creating an infinite async loop
-
Looking to write code that will send data via mqtt perpetually. Learn recently that the publish() method is NOT synchronous, which I augmented by listening to a topic for an ack message before calling the iteration done, and the next iteration publish begins.
So I tried to close the loop by calling the publish() method within the subscribe() handler, and the app crashed with message 'RuntimeError: maximum recursion depth exceeded'.
How do people get around this?
-
Am trying using ONE_SHOT timers. Not sure if this is the recommended approach.
-
could you share your code to here. maybe people can help you.
-
@m5stack I will share pseudo-codes of the current implementation, which is stable so far:
CycleTimer = Timer(-1) def main(): # init and connect mqtt client doCycle() def doCycle(): global CycleTimer # send stuff from mqtt # NOTE: do not use machine.lightsleep() as publish() is NOT synchronous # lightsleep() will block data from being sent CycleTimer.init(period=5000, mode=Timer.ONE_SHOT, callback=doCycle)
NOTE: this is power-intensive; calling lightsleep() will cause uiflow's mqtt client to crash quickly.