@@ -383,9 +383,10 @@ Task
383383 running in different threads. While a task waits for the completion of a
384384 future, the event loop executes a new task.
385385
386- The cancellation of a task is different from the cancelation of a future. Calling
387- :meth: `cancel ` will throw a :exc: `~concurrent.futures.CancelledError ` to the
388- wrapped coroutine. :meth: `~Future.cancelled ` only returns ``True `` if the
386+ The cancellation of a task is different from the cancelation of a
387+ future. Calling :meth: `cancel ` will throw a
388+ :exc: `~concurrent.futures.CancelledError ` to the wrapped
389+ coroutine. :meth: `~Future.cancelled ` only returns ``True `` if the
389390 wrapped coroutine did not catch the
390391 :exc: `~concurrent.futures.CancelledError ` exception, or raised a
391392 :exc: `~concurrent.futures.CancelledError ` exception.
@@ -437,10 +438,11 @@ Task
437438
438439 Return the list of stack frames for this task's coroutine.
439440
440- If the coroutine is not done, this returns the stack where it is suspended.
441- If the coroutine has completed successfully or was cancelled, this
442- returns an empty list. If the coroutine was terminated by an exception,
443- this returns the list of traceback frames.
441+ If the coroutine is not done, this returns the stack where it is
442+ suspended. If the coroutine has completed successfully or was
443+ cancelled, this returns an empty list. If the coroutine was
444+ terminated by an exception, this returns the list of traceback
445+ frames.
444446
445447 The frames are always ordered from oldest to newest.
446448
@@ -582,6 +584,46 @@ Task functions
582584 <coroutine>`, which may be a decorated generator function or an
583585 :keyword: `async def ` function.
584586
587+ .. function :: run_coroutine_threadsafe(coro, loop)
588+
589+ Submit a :ref: `coroutine object <coroutine >` to a given event loop.
590+
591+ Return a :class: `concurrent.futures.Future ` to access the result.
592+
593+ This function is meant to be called from a different thread than the one
594+ where the event loop is running. Usage::
595+
596+ # Create a coroutine
597+ coro = asyncio.sleep(1, result=3)
598+ # Submit the coroutine to a given loop
599+ future = asyncio.run_coroutine_threadsafe(coro, loop)
600+ # Wait for the result with an optional timeout argument
601+ assert future.result(timeout) == 3
602+
603+ If an exception is raised in the coroutine, the returned future will be
604+ notified. It can also be used to cancel the task in the event loop::
605+
606+ try:
607+ result = future.result(timeout)
608+ except asyncio.TimeoutError:
609+ print('The coroutine took too long, cancelling the task...')
610+ future.cancel()
611+ except Exception as exc:
612+ print('The coroutine raised an exception: {!r}'.format(exc))
613+ else:
614+ print('The coroutine returned: {!r}'.format(result))
615+
616+ See the :ref: `concurrency and multithreading <asyncio-multithreading >`
617+ section of the documentation.
618+
619+ .. note ::
620+
621+ Unlike other functions from the module,
622+ :func: `run_coroutine_threadsafe ` requires the *loop * argument to
623+ be passed explicitely.
624+
625+ .. versionadded :: 3.4.4, 3.5.1
626+
585627.. coroutinefunction :: sleep(delay, result=None, \*, loop=None)
586628
587629 Create a :ref: `coroutine <coroutine >` that completes after a given
@@ -620,7 +662,21 @@ Task functions
620662 except CancelledError:
621663 res = None
622664
623- .. coroutinefunction :: wait(futures, \*, loop=None, timeout=None, return_when=ALL_COMPLETED)
665+ .. function :: timeout(timeout, \*, loop=None)
666+
667+ Return a context manager that cancels a block on *timeout * expiring::
668+
669+ with timeout(1.5):
670+ yield from inner()
671+
672+ 1. If ``inner() `` is executed faster than in ``1.5 `` seconds
673+ nothing happens.
674+ 2. Otherwise ``inner() `` is cancelled internally but
675+ :exc: `asyncio.TimeoutError ` is raised outside of
676+ context manager scope.
677+
678+ .. coroutinefunction :: wait(futures, \*, loop=None, timeout=None,\
679+ return_when=ALL_COMPLETED)
624680
625681 Wait for the Futures and coroutine objects given by the sequence *futures *
626682 to complete. Coroutines will be wrapped in Tasks. Returns two sets of
@@ -685,43 +741,3 @@ Task functions
685741
686742 .. versionchanged :: 3.4.3
687743 If the wait is cancelled, the future *fut * is now also cancelled.
688-
689-
690- .. function :: run_coroutine_threadsafe(coro, loop)
691-
692- Submit a :ref: `coroutine object <coroutine >` to a given event loop.
693-
694- Return a :class: `concurrent.futures.Future ` to access the result.
695-
696- This function is meant to be called from a different thread than the one
697- where the event loop is running. Usage::
698-
699- # Create a coroutine
700- coro = asyncio.sleep(1, result=3)
701- # Submit the coroutine to a given loop
702- future = asyncio.run_coroutine_threadsafe(coro, loop)
703- # Wait for the result with an optional timeout argument
704- assert future.result(timeout) == 3
705-
706- If an exception is raised in the coroutine, the returned future will be
707- notified. It can also be used to cancel the task in the event loop::
708-
709- try:
710- result = future.result(timeout)
711- except asyncio.TimeoutError:
712- print('The coroutine took too long, cancelling the task...')
713- future.cancel()
714- except Exception as exc:
715- print('The coroutine raised an exception: {!r}'.format(exc))
716- else:
717- print('The coroutine returned: {!r}'.format(result))
718-
719- See the :ref: `concurrency and multithreading <asyncio-multithreading >`
720- section of the documentation.
721-
722- .. note ::
723-
724- Unlike the functions above, :func: `run_coroutine_threadsafe ` requires the
725- *loop * argument to be passed explicitely.
726-
727- .. versionadded :: 3.5.1
0 commit comments