First of all, to understand what exactly await
does and how task
differs from future
, I recommend starting with this topic and, of course, official documentation.
As for your question, at first glance, the both expressions await coro()
and await create_task(coro())
do the same thing. They start coroutine, wait for it to complete and return the result.
But there are a number of important difference:
- The
await coro()
leads to direct call to the coroutine code without returning execution path to event loop. This issue was explained in this topic.
- The
await create_task(coro())
leads to wrapping the coroutine in a task, scheduling its execution in the event loop, returning execution path to event
loop and then waiting for the result. In this case, before executing of the target coroutine(scheduled as a task) other already sheduled tasks can be executed.
Usually, await
is not used with create_task
, to allow a spawned task to run in parallel, but sometimes it is needed, the example in the next paragraph
- The
await coro()
executes the target coroutine within the current context of variables, and the await create_task(coro())
within the copy of the current context (more details in this topic).
Based on the above, most likely you want await coro()
, leaving the second expression for more specific cases.