@@ -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,8 +192,8 @@ Creating Tasks
140
192
141
193
.. function :: create_task(coro, \*, name=None)
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
If *name * is not ``None ``, it is set as the name of the task using
147
199
:meth: `Task.set_name `.
@@ -150,6 +202,21 @@ Creating Tasks
150
202
:exc: `RuntimeError ` is raised if there is no running loop in
151
203
current thread.
152
204
205
+ This function has been **added in Python 3.7 **. Prior to
206
+ Python 3.7, the low-level :func: `asyncio.ensure_future ` function
207
+ can be used instead::
208
+
209
+ async def coro():
210
+ ...
211
+
212
+ # In Python 3.7+
213
+ task = asyncio.create_task(coro())
214
+ ...
215
+
216
+ # This works in all Python versions but is less readable
217
+ task = asyncio.ensure_future(coro())
218
+ ...
219
+
153
220
.. versionadded :: 3.7
154
221
155
222
.. versionchanged :: 3.8
@@ -166,6 +233,9 @@ Sleeping
166
233
If *result * is provided, it is returned to the caller
167
234
when the coroutine completes.
168
235
236
+ The *loop * argument is deprecated and scheduled for removal
237
+ in Python 4.0.
238
+
169
239
.. _asyncio_example_sleep :
170
240
171
241
Example of coroutine displaying the current date every second
@@ -189,36 +259,31 @@ Sleeping
189
259
Running Tasks Concurrently
190
260
==========================
191
261
192
- .. function :: gather(\*fs, loop=None, return_exceptions=False)
262
+ .. awaitablefunction :: gather(\*fs, loop=None, return_exceptions=False)
193
263
194
- Return a Future aggregating results from the given coroutine objects,
195
- Tasks, or Futures .
264
+ Run :ref: ` awaitable objects < asyncio-awaitables >` in the * fs *
265
+ sequence * concurrently * .
196
266
197
- If all Tasks/Futures are completed successfully, the result is an
198
- aggregate list of returned values. The result values are in the
199
- order of the original *fs * sequence.
267
+ If any awaitable in *fs * is a coroutine, it is automatically
268
+ scheduled as a Task.
200
269
201
- All coroutines in the *fs * list are automatically
202
- scheduled as :class: `Tasks <Task> `.
270
+ If all awaitables are completed successfully, the result is an
271
+ aggregate list of returned values. The order of result values
272
+ corresponds to the order of awaitables in *fs *.
203
273
204
- If *return_exceptions * is ``True ``, exceptions in the Tasks/Futures
205
- are treated the same as successful results, and gathered in the
206
- result list. Otherwise, the first raised exception is immediately
207
- propagated to the returned Future .
274
+ If *return_exceptions * is ``True ``, exceptions are treated the
275
+ same as successful results, and aggregated in the result list.
276
+ Otherwise, the first raised exception is immediately propagated
277
+ to the task that awaits on `` gather() `` .
208
278
209
- If the outer Future is *cancelled *, all submitted Tasks/Futures
279
+ If `` gather `` is *cancelled *, all submitted awaitables
210
280
(that have not completed yet) are also *cancelled *.
211
281
212
- If any child is *cancelled *, it is treated as if it raised
213
- :exc: `CancelledError ` -- the outer Future is **not ** cancelled in
214
- this case. This is to prevent the cancellation of one submitted
215
- Task/Future to cause other Tasks/Futures to be cancelled.
216
-
217
- All futures must share the same event loop.
218
-
219
- .. versionchanged :: 3.7
220
- If the *gather * itself is cancelled, the cancellation is
221
- propagated regardless of *return_exceptions *.
282
+ If any Task or Future from the *fs * sequence is *cancelled *, it is
283
+ treated as if it raised :exc: `CancelledError ` -- the ``gather() ``
284
+ call is **not ** cancelled in this case. This is to prevent the
285
+ cancellation of one submitted Task/Future to cause other
286
+ Tasks/Futures to be cancelled.
222
287
223
288
.. _asyncio_example_gather :
224
289
@@ -235,6 +300,7 @@ Running Tasks Concurrently
235
300
print(f"Task {name}: factorial({number}) = {f}")
236
301
237
302
async def main():
303
+ # Schedule three calls *concurrently*:
238
304
await asyncio.gather(
239
305
factorial("A", 2),
240
306
factorial("B", 3),
@@ -255,17 +321,21 @@ Running Tasks Concurrently
255
321
# Task C: Compute factorial(4)...
256
322
# Task C: factorial(4) = 24
257
323
324
+ .. versionchanged :: 3.7
325
+ If the *gather * itself is cancelled, the cancellation is
326
+ propagated regardless of *return_exceptions *.
327
+
258
328
259
329
Shielding Tasks From Cancellation
260
330
=================================
261
331
262
- .. coroutinefunction :: shield(fut, \*, loop=None)
332
+ .. awaitablefunction :: shield(fut, \*, loop=None)
263
333
264
- Wait for a Future/Task while protecting it from being cancelled.
334
+ Protect an :ref: `awaitable object <asyncio-awaitables >`
335
+ from being :meth: `cancelled <Task.cancel> `.
265
336
266
337
*fut * can be a coroutine, a Task, or a Future-like object. If
267
- *fut * is a coroutine it is automatically scheduled as a
268
- :class: `Task `.
338
+ *fut * is a coroutine it is automatically scheduled as a Task.
269
339
270
340
The statement::
271
341
@@ -299,11 +369,10 @@ Timeouts
299
369
300
370
.. coroutinefunction :: wait_for(fut, timeout, \*, loop=None)
301
371
302
- Wait for a coroutine, Task, or Future to complete with timeout.
372
+ Wait for the *fut * :ref: `awaitable <asyncio-awaitables >`
373
+ to complete with a timeout.
303
374
304
- *fut * can be a coroutine, a Task, or a Future-like object. If
305
- *fut * is a coroutine it is automatically scheduled as a
306
- :class: `Task `.
375
+ If *fut * is a coroutine it is automatically scheduled as a Task.
307
376
308
377
*timeout * can either be ``None `` or a float or int number of seconds
309
378
to wait for. If *timeout * is ``None ``, block until the future
@@ -312,13 +381,17 @@ Timeouts
312
381
If a timeout occurs, it cancels the task and raises
313
382
:exc: `asyncio.TimeoutError `.
314
383
315
- To avoid the task cancellation, wrap it in :func: `shield `.
384
+ To avoid the task :meth: `cancellation <Task.cancel> `,
385
+ wrap it in :func: `shield `.
316
386
317
387
The function will wait until the future is actually cancelled,
318
388
so the total wait time may exceed the *timeout *.
319
389
320
390
If the wait is cancelled, the future *fut * is also cancelled.
321
391
392
+ The *loop * argument is deprecated and scheduled for removal
393
+ in Python 4.0.
394
+
322
395
.. _asyncio_example_waitfor :
323
396
324
397
Example::
@@ -353,13 +426,18 @@ Waiting Primitives
353
426
.. coroutinefunction :: wait(fs, \*, loop=None, timeout=None,\
354
427
return_when=ALL_COMPLETED)
355
428
356
- Wait for a set of coroutines, Tasks, or Futures to complete.
429
+ Run :ref: `awaitable objects <asyncio-awaitables >` in the *fs *
430
+ sequence concurrently and block until the condition specified
431
+ by *return_when *.
357
432
358
- *fs * is a list of coroutines, Futures, and/or Tasks. Coroutines
359
- are automatically scheduled as :class: ` Tasks < Task> ` .
433
+ If any awaitable in *fs * is a coroutine, it is automatically
434
+ scheduled as a Task.
360
435
361
436
Returns two sets of Tasks/Futures: ``(done, pending) ``.
362
437
438
+ The *loop * argument is deprecated and scheduled for removal
439
+ in Python 4.0.
440
+
363
441
*timeout * (a float or int), if specified, can be used to control
364
442
the maximum number of seconds to wait before returning.
365
443
@@ -398,16 +476,18 @@ Waiting Primitives
398
476
399
477
.. function :: as_completed(fs, \*, loop=None, timeout=None)
400
478
401
- Return an iterator of awaitables which return
402
- :class: `Future ` instances.
479
+ Run :ref: `awaitable objects <asyncio-awaitables >` in the *fs *
480
+ set concurrently. Return an iterator of :class: `Future ` objects.
481
+ Each Future object returned represents the earliest result
482
+ from the set of the remaining awaitables.
403
483
404
484
Raises :exc: `asyncio.TimeoutError ` if the timeout occurs before
405
485
all Futures are done.
406
486
407
487
Example::
408
488
409
489
for f in as_completed(fs):
410
- result = await f
490
+ earliest_result = await f
411
491
# ...
412
492
413
493
@@ -418,7 +498,8 @@ Scheduling From Other Threads
418
498
419
499
Submit a coroutine to the given event loop. Thread-safe.
420
500
421
- Return a :class: `concurrent.futures.Future ` to access the result.
501
+ Return a :class: `concurrent.futures.Future ` to wait for the result
502
+ from another OS thread.
422
503
423
504
This function is meant to be called from a different OS thread
424
505
than the one where the event loop is running. Example::
0 commit comments