frazman
frazman

Reputation: 33213

Using wait_for with timeouts with list of tasks

So, I have a list of tasks which I want to schedule concurrently in a non-blocking fashion. Basically, gather should do the trick. Like

tasks = [ asyncio.create_task(some_task()) in bleh]
results = await asyncio.gather(*tasks)

But then, I also need a timeout. What I want is that any task which takes > timeout time cancels and I proceed with what I have.

I fould asyncio.wait primitive. https://docs.python.org/3/library/asyncio-task.html#waiting-primitives

But then the doc says: Run awaitable objects in the aws set concurrently and block until the condition specified by return_when.

Which seems to suggest that it blocks...

It seems that asyncio.wait_for will do the trick https://docs.python.org/3/library/asyncio-task.html#timeouts But how do i send in the list of awaitables rather than just an awaitable?

Upvotes: 0

Views: 1215

Answers (1)

user4815162342
user4815162342

Reputation: 154836

What I want is that any task which takes > timeout time cancels and I proceed with what I have.

This is straightforward to achieve with asyncio.wait():

# Wait for tasks to finish, but no more than a second.
done, pending = await asyncio.wait(tasks, timeout=1)
# Cancel the ones not done by now.
for fut in pending:
    fut.cancel()
# Results are available as x.result() on futures in `done`

Which seems to suggest that [asyncio.wait] blocks...

It only blocks the current coroutine, the same as gather or wait_for.

Upvotes: 1

Related Questions