'Run until complete and asyncio gather throwing a runtime warning
I am creating a program where a new Class is instantiated, let's call it Profile, and then data is streamed that would update the profile. Periodically the periodic function is also supposed to perform a checkup inside the instantiated profile, to make sure things are in order.
def _run():
stream = Stream(...)
api = API(...)
try:
_LOG.info("Generating Data.")
...
async def periodic():
while True:
if not api.get_clock().is_open:
raise RuntimeError()
await asyncio.sleep(60)
items = api.get_items()
for item in items:
...class.do_checkup(item)...
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
stream.run(),
periodic(),
))
except KeyboardInterrupt:
# stream.stop()
# loop.close()
loop.stop()
exit(0)
except RuntimeError:
_LOG.info('...')
except Exception as e:
_LOG.error(e)
finally:
_LOG.info("Trying to re-establish connection.")
time.sleep(3)
# TODO: restart here
# _run()
The code above though, when trying to stop it, complains that:
UP (BASE): Generating Data.
...
.../__main__.py:89: RuntimeWarning: coroutine '_run.<locals>.periodic' was never awaited
_LOG.error(e)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
keyboard interrupt, bye
UP (BASE): An asyncio.Future, a coroutine or an awaitable is required
What is the correct way to have asyncio run periodic correctly?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
