Difference between coroutine and future/task in Python 3.5?
Let's say we have a dummy function:
async def foo(arg): result = await some_remote_call(arg) return result.upper()
What's the difference between:
coros =  for i in range(5): coros.append(foo(i)) loop = get_event_loop() loop.run_until_complete(wait(coros))
from asyncio import ensure_future futures =  for i in range(5): futures.append(ensure_future(foo(i))) loop = get_event_loop() loop.run_until_complete(wait(futures))
Note : The example returns a result, but this isn't the focus of the question. When return value matters, use
gather() instead of
Regardless of return value, I'm looking for clarity on
wait(futures) both run the coroutines, so when and why should a coroutine be wrapped in
Basically, what's the Right Way (tm) to run a bunch of non-blocking operations using Python 3.5's
For extra credit, what if I want to batch the calls? For example, I need to call
some_remote_call(...) 1000 times, but I don't want to crush the web server/database/etc with 1000 simultaneous connections. This is doable with a thread or process pool, but is there a way to do this with
A comment by Vincent linked to https://github.com/python/asyncio/blob/master/asyncio/tasks.py#L346, which shows that
wait() wraps the coroutines in
ensure_future() for you!
In other words, we do need a future, and coroutines will be silently transformed into them.
I'll update this answer when I find a definitive explanation of how to batch coroutines/futures.
★ Back to homepage or read more recommendations: