@@ -466,14 +466,20 @@ Waiting Primitives
466466 return_when=ALL_COMPLETED)
467467
468468 Run :ref: `awaitable objects <asyncio-awaitables >` in the *aws *
469- sequence concurrently and block until the condition specified
469+ set concurrently and block until the condition specified
470470 by *return_when *.
471471
472472 If any awaitable in *aws * is a coroutine, it is automatically
473- scheduled as a Task.
473+ scheduled as a Task. Passing coroutines objects to
474+ ``wait() `` directly is deprecated as it leads to
475+ :ref: `confusing behavior <asyncio_example_wait_coroutine >`.
474476
475477 Returns two sets of Tasks/Futures: ``(done, pending) ``.
476478
479+ Usage::
480+
481+ done, pending = await asyncio.wait(aws)
482+
477483 The *loop * argument is deprecated and scheduled for removal
478484 in Python 4.0.
479485
@@ -508,9 +514,35 @@ Waiting Primitives
508514 Unlike :func: `~asyncio.wait_for `, ``wait() `` does not cancel the
509515 futures when a timeout occurs.
510516
511- Usage::
517+ .. _asyncio_example_wait_coroutine :
518+ .. note ::
512519
513- done, pending = await asyncio.wait(aws)
520+ ``wait() `` schedules coroutines as Tasks automatically and later
521+ returns those implicitly created Task objects in ``(done, pending) ``
522+ sets. Therefore the following code won't work as expected::
523+
524+ async def foo():
525+ return 42
526+
527+ coro = foo()
528+ done, pending = await asyncio.wait({coro})
529+
530+ if coro in done:
531+ # This branch will never be run!
532+
533+ Here is how the above snippet can be fixed::
534+
535+ async def foo():
536+ return 42
537+
538+ task = asyncio.create_task(foo())
539+ done, pending = await asyncio.wait({task})
540+
541+ if task in done:
542+ # Everything will work as expected now.
543+
544+ Passing coroutine objects to ``wait() `` directly is
545+ deprecated.
514546
515547
516548.. function :: as_completed(aws, \*, loop=None, timeout=None)
0 commit comments