Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

gather unexpectedly schedules coroutines in undeterministic order #432

@JustinTArthur

Description

@JustinTArthur

If you pass awaitables or coroutines to asyncio.gather, the function takes responsibility for wrapping them in futures and scheduling their execution. The order in which they are scheduled for execution is non-deterministic due to gather coercing the arguments to a unique set to ensure the same coroutine object isn't undertaken more than once.

Example:

import asyncio

order = []
@asyncio.coroutine
def append_a_thing(thing):
    order.append(thing)
    return thing

loop = asyncio.get_event_loop()
task = asyncio.gather(append_a_thing(1), append_a_thing(2), append_a_thing(3), append_a_thing(4))
loop.run_until_complete(results)

print("Things appended were {}.".format(task.result()))
print("Order of execution was {}.".format(order))

This might print:

Things appended were [1, 2, 3, 4].
Order of execution was [3, 1, 2, 4].

Either this is cool, and we should just warn the programmer that their coroutines may be scheduled (and executed) in any order their Python interpreter chooses…
or it's not cool and we should schedule tasks in the order expected by the programmer, while maintaining that unique awaitables and coroutines are only scheduled for execution once.

I'll submit a pull request for either scenario and someone else can decide what's cool.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions