Skip to content

Commit ff804fe

Browse files
committed
Use asyncio.create_task instead of asyncio.Task
1 parent 0dd99eb commit ff804fe

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

Doc/howto/a-conceptual-overview-of-asyncio.rst

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,17 @@ Roughly speaking, :ref:`tasks <asyncio-task-obj>` are coroutines (not coroutine
159159
functions) tied to an event loop.
160160
A task also maintains a list of callback functions whose importance will become
161161
clear in a moment when we discuss ``await``.
162-
When tasks are created they are automatically added to the event loop's queue
163-
of tasks::
162+
The recommended way to create tasks is via :func:`asyncio.create_task`.
163+
Creating a task automatically adds it to the event loop's queue of tasks.
164164

165-
# This creates a Task object and puts it on the event loop's queue.
166-
special_task = asyncio.Task(coro=special_fella(magic_number=5), loop=event_loop)
165+
Since there's only one event loop (in each thread), ``asyncio`` takes care of
166+
associating the task with the event loop for you. That is, there's no need
167+
to specify the event loop.
167168

168-
It's common to see a task instantiated without explicitly specifying the event loop
169-
it belongs to.
170-
Since there's only one event loop (in each thread), asyncio made the loop argument optional and
171-
will add it for you if it's left unspecified::
169+
::
172170

173-
# This creates another Task object and puts it on the event loop's queue.
174-
# The task is implicitly tied to the event loop by asyncio since the
175-
# loop argument was left unspecified.
176-
another_special_task = asyncio.Task(coro=special_fella(magic_number=12))
171+
# This creates a Task object and puts it on the event loop's queue.
172+
special_task = asyncio.create_task(coro=special_fella(magic_number=5))
177173

178174
=====
179175
await
@@ -214,7 +210,7 @@ Consider this program::
214210
print("I am coro_b(). I sure hope no one hogs the event loop...")
215211

216212
async def main():
217-
task_b = asyncio.Task(coro_b())
213+
task_b = asyncio.create_task(coro_b())
218214
num_repeats = 3
219215
for _ in range(num_repeats):
220216
await coro_a()
@@ -235,7 +231,7 @@ invocations before ``coro_b()``'s output:
235231
I am coro_a(). Hi!
236232
I am coro_b(). I sure hope no one hogs the event loop...
237233
238-
If we change ``await coro_a()`` to ``await asyncio.Task(coro_a())``, the
234+
If we change ``await coro_a()`` to ``await asyncio.create_task(coro_a())``, the
239235
behavior changes.
240236
The coroutine ``main()`` cedes control to the event loop with that statement.
241237
The event loop then works through its queue, calling ``coro_b()`` and then
@@ -392,15 +388,15 @@ hogging control while waiting.
392388
# Add a few other tasks to the event loop, so there's something
393389
# to do while asynchronously sleeping.
394390
work_tasks = [
395-
asyncio.Task(other_work()),
396-
asyncio.Task(other_work()),
397-
asyncio.Task(other_work())
391+
asyncio.create_task(other_work()),
392+
asyncio.create_task(other_work()),
393+
asyncio.create_task(other_work())
398394
]
399395
print(
400396
"Beginning asynchronous sleep at time: "
401397
f"{datetime.datetime.now().strftime("%H:%M:%S")}."
402398
)
403-
await asyncio.Task(async_sleep(3))
399+
await asyncio.create_task(async_sleep(3))
404400
print(
405401
"Done asynchronous sleep at time: "
406402
f"{datetime.datetime.now().strftime("%H:%M:%S")}."
@@ -423,7 +419,7 @@ will monitor how much time has elapsed and accordingly call
423419
future = asyncio.Future()
424420
time_to_wake = time.time() + seconds
425421
# Add the watcher-task to the event loop.
426-
watcher_task = asyncio.Task(_sleep_watcher(future, time_to_wake))
422+
watcher_task = asyncio.create_task(_sleep_watcher(future, time_to_wake))
427423
# Block until the future is marked as done.
428424
await future
429425

0 commit comments

Comments
 (0)