I have multiple coroutines which need to be running (forever) at the same time. For error handling one of the routines occasionally ends and needs to be respawned and I use the following code, which however assumes that it is coroutine1 that needs restarting.
pending = {coroutine1(), coroutine2()}
while True:
a = asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
done, pending = loop.run_until_complete(a)
pending = pending | {coroutine1()}
How can I solve this in a better and more general way?