3

A common pattern with asyncio, like the one shown here, is to add a collection of coroutines to a list, and then asyncio.gather them.

For instance:

async def some_task(i):
    # Do something asynchronously with i

tasks = [some_task(i) for i in range(100)]

loop.run_until_complete(asyncio.gather(**tasks))

Here, the execution order of this code is such that none of the tasks are running while we build up the list. We add task 1 to the list, then task 2, etc. and then we add the tasks 1-100 to the event loop.


However, I want task creation itself to be part of the event loop. I want task 1 to be scheduled immediately as it's created, and then when task is waiting for something on another thread, return to task creation and create task 2 and add it to the event loop.

I believe this would give me better concurrency from my async code. Is this possible?


For example, my first thought would be to put task creation into a coroutine and schedule tasks as they are created:

async def some_task(i):
    # Do something asynchronously with i

async def generate_tasks(loop):
    tasks = []
    for i in range(100):
        task = loop.create_task(some_task(i))
        tasks.append(loop)
    await asyncio.gather(**tasks)

loop.run_until_complete(generate_tasks())

However, because my generate_tasks never uses await, execution is never passed back to the event loop, so the entirety of generate_tasks will run before some_task() is run at all.


But then, if I await each task as they are created, it will wait for each task to complete before moving on to the next task, giving me no concurrency at all!

async def generate_tasks(loop):
    tasks = []
    for i in range(100):
        await some_task(i)

loop.run_until_complete(generate_tasks())
2
  • It won't give you better concurrency. When you execute tasks = [some_task(i) for i in range(100)], all 100 tasks have been created. some_task(i) returns a task object. And what asyncio.gather does is adding all tasks into loop and awaiting all of them. So you won't get better concurrency. Commented Jun 26, 2018 at 15:04
  • But that creation of tasks (the list comprehension) is entirely serial. If task creation were part of the event loop I'd get some parallelism (as much as you can using async) Commented Jun 27, 2018 at 4:03

1 Answer 1

1

However, because my generate_tasks never uses await, execution is never passed back to the event loop

You can use await asyncio.sleep(0) to force yielding to the event loop inside for. But that is unlikely to make a difference, creating a task/coroutine pair is really efficient.

Before optimizing this, measure (with something as simple as time.time if need be) how much time it takes to execute the [some_task(i) for i in range(100)] list comprehension. Then consider whether dispersing that time (possibly making it take longer to finish due to increased scheduling overhead) will make any difference for your application. The results might surprise you.

Sign up to request clarification or add additional context in comments.

2 Comments

So I could task = loop.create_task(some_task(i)); tasks.append(loop); await asyncio.sleep(0) to create the task, add it to the event loop, and then execute it immediately? That sounds like what I want. I will check performance but this still solves what I asked about
@Miguel Yes, except there should be no need to do that. asyncio is about scheduling around timeouts and blocking network calls. Creating a task (or 100 of them) should be pretty much instantaneous, and adding spurious context switches could actually slow things down.

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.