2

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?

1 Answer 1

1

What about using a different approach?

async def run_forever(corofn):
    while True:
        await corofn()

corofns = coroutine1, coroutine2
await asyncio.wait(map(run_forever, corofns))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.