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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login