gather unexpectedly schedules coroutines in undeterministic order
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.