I am trying to implement a micropython Class to make a function runs forever at a certain time interval. For that I am trying to adapt the following code from this post:
class Timer:
def __init__(self, timeout, callback):
self._timeout = timeout
self._callback = callback
self._task = asyncio.create_task(self._job())
async def _job(self):
while True:
await asyncio.sleep(self._timeout)
await self._callback()
def cancel(self):
self._task.cancel()
async def timeout_callback():
await asyncio.sleep(0.1)
print('echo!')
async def main():
timer = Timer(2, timeout_callback) # set timer for two seconds
await asyncio.sleep(12.5) # wait to see timer works
asyncio.run(main())
The point is, I can't make it run indefinitely as a "normal" while loop. I have notice that the callback keeps running as per the time defines in the last await asyncio.sleep(secs)
command... but this is not what I actually want to achieve. Any help would be hilly appreciated.