@@ -103,6 +103,31 @@ To actually run a coroutine asyncio provides three main mechanisms:
103
103
world
104
104
finished at 17:14:34
105
105
106
+
107
+ .. _asyncio-awaitables :
108
+
109
+ Awaitables
110
+ ==========
111
+
112
+ We say that an object is an *awaitable * object if it can be used
113
+ in an :keyword: `await ` expression.
114
+
115
+
116
+ .. rubric :: Coroutines and Tasks
117
+
118
+ Python coroutines are *awaitables *::
119
+
120
+ async def nested():
121
+ return 42
122
+
123
+ async def main():
124
+ # Will print "42":
125
+ print(await nested())
126
+
127
+ *Tasks * are used to schedule coroutines *concurrently *.
128
+ See the previous :ref: `section <coroutine >` for an introduction
129
+ to coroutines and tasks.
130
+
106
131
Note that in this documentation the term "coroutine" can be used for
107
132
two closely related concepts:
108
133
@@ -112,14 +137,41 @@ two closely related concepts:
112
137
*coroutine function *.
113
138
114
139
140
+ .. rubric :: Futures
141
+
142
+ There is a dedicated section about the :ref: `asyncio Future object
143
+ <asyncio-futures>`, but the concept is fundamental to asyncio so
144
+ it needs a brief introduction in this section.
145
+
146
+ A Future is a special **low-level ** awaitable object that represents
147
+ an **eventual result ** of an asynchronous operation.
148
+ Future objects in asyncio are needed to allow callback-based code
149
+ to be used with async/await.
150
+
151
+ Normally, **there is no need ** to create Future objects at the
152
+ application level code.
153
+
154
+ Future objects, sometimes exposed by libraries and some asyncio
155
+ APIs, should be awaited::
156
+
157
+ async def main():
158
+ await function_that_returns_a_future_object()
159
+
160
+ # this is also valid:
161
+ await asyncio.gather(
162
+ function_that_returns_a_future_object(),
163
+ some_python_coroutine()
164
+ )
165
+
166
+
115
167
Running an asyncio Program
116
168
==========================
117
169
118
170
.. function :: run(coro, \*, debug=False)
119
171
120
172
This function runs the passed coroutine, taking care of
121
- managing the asyncio event loop and finalizing asynchronous
122
- generators.
173
+ managing the asyncio event loop and * finalizing asynchronous
174
+ generators * .
123
175
124
176
This function cannot be called when another asyncio event loop is
125
177
running in the same thread.
@@ -140,13 +192,28 @@ Creating Tasks
140
192
141
193
.. function :: create_task(coro)
142
194
143
- Wrap the *coro * :ref: `coroutine <coroutine >` into a task and schedule
144
- its execution. Return the task object.
195
+ Wrap the *coro * :ref: `coroutine <coroutine >` into a Task and
196
+ schedule its execution. Return the Task object.
145
197
146
198
The task is executed in the loop returned by :func: `get_running_loop `,
147
199
:exc: `RuntimeError ` is raised if there is no running loop in
148
200
current thread.
149
201
202
+ This function has been **added in Python 3.7 **. Prior to
203
+ Python 3.7, the low-level :func: `asyncio.ensure_future ` function
204
+ can be used instead::
205
+
206
+ async def coro():
207
+ ...
208
+
209
+ # In Python 3.7+
210
+ task = asyncio.create_task(coro())
211
+ ...
212
+
213
+ # This works in all Python versions but is less readable
214
+ task = asyncio.ensure_future(coro())
215
+ ...
216
+
150
217
.. versionadded :: 3.7
151
218
152
219
@@ -160,6 +227,9 @@ Sleeping
160
227
If *result * is provided, it is returned to the caller
161
228
when the coroutine completes.
162
229
230
+ The *loop * argument is deprecated and scheduled for removal
231
+ in Python 4.0.
232
+
163
233
.. _asyncio_example_sleep :
164
234
165
235
Example of coroutine displaying the current date every second
@@ -183,36 +253,31 @@ Sleeping
183
253
Running Tasks Concurrently
184
254
==========================
185
255
186
- .. function :: gather(\*fs, loop=None, return_exceptions=False)
256
+ .. awaitablefunction :: gather(\*fs, loop=None, return_exceptions=False)
187
257
188
- Return a Future aggregating results from the given coroutine objects,
189
- Tasks, or Futures .
258
+ Run :ref: ` awaitable objects < asyncio-awaitables >` in the * fs *
259
+ sequence * concurrently * .
190
260
191
- If all Tasks/Futures are completed successfully, the result is an
192
- aggregate list of returned values. The result values are in the
193
- order of the original *fs * sequence.
261
+ If any awaitable in *fs * is a coroutine, it is automatically
262
+ scheduled as a Task.
194
263
195
- All coroutines in the *fs * list are automatically
196
- scheduled as :class: `Tasks <Task> `.
264
+ If all awaitables are completed successfully, the result is an
265
+ aggregate list of returned values. The order of result values
266
+ corresponds to the order of awaitables in *fs *.
197
267
198
- If *return_exceptions * is ``True ``, exceptions in the Tasks/Futures
199
- are treated the same as successful results, and gathered in the
200
- result list. Otherwise, the first raised exception is immediately
201
- propagated to the returned Future .
268
+ If *return_exceptions * is ``True ``, exceptions are treated the
269
+ same as successful results, and aggregated in the result list.
270
+ Otherwise, the first raised exception is immediately propagated
271
+ to the task that awaits on `` gather() `` .
202
272
203
- If the outer Future is *cancelled *, all submitted Tasks/Futures
273
+ If `` gather `` is *cancelled *, all submitted awaitables
204
274
(that have not completed yet) are also *cancelled *.
205
275
206
- If any child is *cancelled *, it is treated as if it raised
207
- :exc: `CancelledError ` -- the outer Future is **not ** cancelled in
208
- this case. This is to prevent the cancellation of one submitted
209
- Task/Future to cause other Tasks/Futures to be cancelled.
210
-
211
- All futures must share the same event loop.
212
-
213
- .. versionchanged :: 3.7
214
- If the *gather * itself is cancelled, the cancellation is
215
- propagated regardless of *return_exceptions *.
276
+ If any Task or Future from the *fs * sequence is *cancelled *, it is
277
+ treated as if it raised :exc: `CancelledError ` -- the ``gather() ``
278
+ call is **not ** cancelled in this case. This is to prevent the
279
+ cancellation of one submitted Task/Future to cause other
280
+ Tasks/Futures to be cancelled.
216
281
217
282
.. _asyncio_example_gather :
218
283
@@ -229,6 +294,7 @@ Running Tasks Concurrently
229
294
print(f"Task {name}: factorial({number}) = {f}")
230
295
231
296
async def main():
297
+ # Schedule three calls *concurrently*:
232
298
await asyncio.gather(
233
299
factorial("A", 2),
234
300
factorial("B", 3),
@@ -249,17 +315,21 @@ Running Tasks Concurrently
249
315
# Task C: Compute factorial(4)...
250
316
# Task C: factorial(4) = 24
251
317
318
+ .. versionchanged :: 3.7
319
+ If the *gather * itself is cancelled, the cancellation is
320
+ propagated regardless of *return_exceptions *.
321
+
252
322
253
323
Shielding Tasks From Cancellation
254
324
=================================
255
325
256
- .. coroutinefunction :: shield(fut, \*, loop=None)
326
+ .. awaitablefunction :: shield(fut, \*, loop=None)
257
327
258
- Wait for a Future/Task while protecting it from being cancelled.
328
+ Protect an :ref: `awaitable object <asyncio-awaitables >`
329
+ from being :meth: `cancelled <Task.cancel> `.
259
330
260
331
*fut * can be a coroutine, a Task, or a Future-like object. If
261
- *fut * is a coroutine it is automatically scheduled as a
262
- :class: `Task `.
332
+ *fut * is a coroutine it is automatically scheduled as a Task.
263
333
264
334
The statement::
265
335
@@ -293,11 +363,10 @@ Timeouts
293
363
294
364
.. coroutinefunction :: wait_for(fut, timeout, \*, loop=None)
295
365
296
- Wait for a coroutine, Task, or Future to complete with timeout.
366
+ Wait for the *fut * :ref: `awaitable <asyncio-awaitables >`
367
+ to complete with a timeout.
297
368
298
- *fut * can be a coroutine, a Task, or a Future-like object. If
299
- *fut * is a coroutine it is automatically scheduled as a
300
- :class: `Task `.
369
+ If *fut * is a coroutine it is automatically scheduled as a Task.
301
370
302
371
*timeout * can either be ``None `` or a float or int number of seconds
303
372
to wait for. If *timeout * is ``None ``, block until the future
@@ -306,13 +375,17 @@ Timeouts
306
375
If a timeout occurs, it cancels the task and raises
307
376
:exc: `asyncio.TimeoutError `.
308
377
309
- To avoid the task cancellation, wrap it in :func: `shield `.
378
+ To avoid the task :meth: `cancellation <Task.cancel> `,
379
+ wrap it in :func: `shield `.
310
380
311
381
The function will wait until the future is actually cancelled,
312
382
so the total wait time may exceed the *timeout *.
313
383
314
384
If the wait is cancelled, the future *fut * is also cancelled.
315
385
386
+ The *loop * argument is deprecated and scheduled for removal
387
+ in Python 4.0.
388
+
316
389
.. _asyncio_example_waitfor :
317
390
318
391
Example::
@@ -347,13 +420,18 @@ Waiting Primitives
347
420
.. coroutinefunction :: wait(fs, \*, loop=None, timeout=None,\
348
421
return_when=ALL_COMPLETED)
349
422
350
- Wait for a set of coroutines, Tasks, or Futures to complete.
423
+ Run :ref: `awaitable objects <asyncio-awaitables >` in the *fs *
424
+ sequence concurrently and block until the condition specified
425
+ by *return_when *.
351
426
352
- *fs * is a list of coroutines, Futures, and/or Tasks. Coroutines
353
- are automatically scheduled as :class: ` Tasks < Task> ` .
427
+ If any awaitable in *fs * is a coroutine, it is automatically
428
+ scheduled as a Task.
354
429
355
430
Returns two sets of Tasks/Futures: ``(done, pending) ``.
356
431
432
+ The *loop * argument is deprecated and scheduled for removal
433
+ in Python 4.0.
434
+
357
435
*timeout * (a float or int), if specified, can be used to control
358
436
the maximum number of seconds to wait before returning.
359
437
@@ -392,16 +470,18 @@ Waiting Primitives
392
470
393
471
.. function :: as_completed(fs, \*, loop=None, timeout=None)
394
472
395
- Return an iterator of awaitables which return
396
- :class: `Future ` instances.
473
+ Run :ref: `awaitable objects <asyncio-awaitables >` in the *fs *
474
+ set concurrently. Return an iterator of :class: `Future ` objects.
475
+ Each Future object returned represents the earliest result
476
+ from the set of the remaining awaitables.
397
477
398
478
Raises :exc: `asyncio.TimeoutError ` if the timeout occurs before
399
479
all Futures are done.
400
480
401
481
Example::
402
482
403
483
for f in as_completed(fs):
404
- result = await f
484
+ earliest_result = await f
405
485
# ...
406
486
407
487
@@ -412,7 +492,8 @@ Scheduling From Other Threads
412
492
413
493
Submit a coroutine to the given event loop. Thread-safe.
414
494
415
- Return a :class: `concurrent.futures.Future ` to access the result.
495
+ Return a :class: `concurrent.futures.Future ` to wait for the result
496
+ from another OS thread.
416
497
417
498
This function is meant to be called from a different OS thread
418
499
than the one where the event loop is running. Example::
0 commit comments