Skip to content

Commit b0f5401

Browse files
committed
Document asyncio.timeout()
1 parent 7ec07a1 commit b0f5401

File tree

1 file changed

+63
-46
lines changed

1 file changed

+63
-46
lines changed

Doc/library/asyncio-task.rst

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,10 @@ Task
363363
running in different threads. While a task waits for the completion of a
364364
future, the event loop executes a new task.
365365

366-
The cancellation of a task is different from the cancelation of a future. Calling
367-
:meth:`cancel` will throw a :exc:`~concurrent.futures.CancelledError` to the
368-
wrapped coroutine. :meth:`~Future.cancelled` only returns ``True`` if the
366+
The cancellation of a task is different from the cancelation of a
367+
future. Calling :meth:`cancel` will throw a
368+
:exc:`~concurrent.futures.CancelledError` to the wrapped
369+
coroutine. :meth:`~Future.cancelled` only returns ``True`` if the
369370
wrapped coroutine did not catch the
370371
:exc:`~concurrent.futures.CancelledError` exception, or raised a
371372
:exc:`~concurrent.futures.CancelledError` exception.
@@ -417,10 +418,11 @@ Task
417418

418419
Return the list of stack frames for this task's coroutine.
419420

420-
If the coroutine is not done, this returns the stack where it is suspended.
421-
If the coroutine has completed successfully or was cancelled, this
422-
returns an empty list. If the coroutine was terminated by an exception,
423-
this returns the list of traceback frames.
421+
If the coroutine is not done, this returns the stack where it is
422+
suspended. If the coroutine has completed successfully or was
423+
cancelled, this returns an empty list. If the coroutine was
424+
terminated by an exception, this returns the list of traceback
425+
frames.
424426

425427
The frames are always ordered from oldest to newest.
426428

@@ -557,6 +559,45 @@ Task functions
557559
Return ``True`` if *func* is a decorated :ref:`coroutine function
558560
<coroutine>`.
559561

562+
.. function:: run_coroutine_threadsafe(coro, loop)
563+
564+
Submit a :ref:`coroutine object <coroutine>` to a given event loop.
565+
566+
Return a :class:`concurrent.futures.Future` to access the result.
567+
568+
This function is meant to be called from a different thread than the one
569+
where the event loop is running. Usage::
570+
571+
# Create a coroutine
572+
coro = asyncio.sleep(1, result=3)
573+
# Submit the coroutine to a given loop
574+
future = asyncio.run_coroutine_threadsafe(coro, loop)
575+
# Wait for the result with an optional timeout argument
576+
assert future.result(timeout) == 3
577+
578+
If an exception is raised in the coroutine, the returned future will be
579+
notified. It can also be used to cancel the task in the event loop::
580+
581+
try:
582+
result = future.result(timeout)
583+
except asyncio.TimeoutError:
584+
print('The coroutine took too long, cancelling the task...')
585+
future.cancel()
586+
except Exception as exc:
587+
print('The coroutine raised an exception: {!r}'.format(exc))
588+
else:
589+
print('The coroutine returned: {!r}'.format(result))
590+
591+
See the :ref:`concurrency and multithreading <asyncio-multithreading>`
592+
section of the documentation.
593+
594+
.. note::
595+
596+
Unlike the functions above, :func:`run_coroutine_threadsafe` requires the
597+
*loop* argument to be passed explicitely.
598+
599+
.. versionadded:: 3.4.4, 3.5.1
600+
560601
.. coroutinefunction:: sleep(delay, result=None, \*, loop=None)
561602

562603
Create a :ref:`coroutine <coroutine>` that completes after a given
@@ -595,7 +636,21 @@ Task functions
595636
except CancelledError:
596637
res = None
597638

598-
.. coroutinefunction:: wait(futures, \*, loop=None, timeout=None, return_when=ALL_COMPLETED)
639+
.. function:: timeout(timeout, \*, loop=None)
640+
641+
Return a context manager that cancels a block on *timeout* expiring::
642+
643+
with timeout(1.5):
644+
yield from inner()
645+
646+
1. If ``inner()`` is executed faster than in ``1.5`` seconds
647+
nothing happens.
648+
2. Otherwise ``inner()`` is cancelled internally but
649+
:exc:`asyncio.TimeoutError` is raised outside of
650+
context manager scope.
651+
652+
.. coroutinefunction:: wait(futures, \*, loop=None, timeout=None,\
653+
return_when=ALL_COMPLETED)
599654

600655
Wait for the Futures and coroutine objects given by the sequence *futures*
601656
to complete. Coroutines will be wrapped in Tasks. Returns two sets of
@@ -662,41 +717,3 @@ Task functions
662717
If the wait is cancelled, the future *fut* is now also cancelled.
663718

664719

665-
.. function:: run_coroutine_threadsafe(coro, loop)
666-
667-
Submit a :ref:`coroutine object <coroutine>` to a given event loop.
668-
669-
Return a :class:`concurrent.futures.Future` to access the result.
670-
671-
This function is meant to be called from a different thread than the one
672-
where the event loop is running. Usage::
673-
674-
# Create a coroutine
675-
coro = asyncio.sleep(1, result=3)
676-
# Submit the coroutine to a given loop
677-
future = asyncio.run_coroutine_threadsafe(coro, loop)
678-
# Wait for the result with an optional timeout argument
679-
assert future.result(timeout) == 3
680-
681-
If an exception is raised in the coroutine, the returned future will be
682-
notified. It can also be used to cancel the task in the event loop::
683-
684-
try:
685-
result = future.result(timeout)
686-
except asyncio.TimeoutError:
687-
print('The coroutine took too long, cancelling the task...')
688-
future.cancel()
689-
except Exception as exc:
690-
print('The coroutine raised an exception: {!r}'.format(exc))
691-
else:
692-
print('The coroutine returned: {!r}'.format(result))
693-
694-
See the :ref:`concurrency and multithreading <asyncio-multithreading>`
695-
section of the documentation.
696-
697-
.. note::
698-
699-
Unlike the functions above, :func:`run_coroutine_threadsafe` requires the
700-
*loop* argument to be passed explicitely.
701-
702-
.. versionadded:: 3.4.4, 3.5.1

0 commit comments

Comments
 (0)