Docs and one small improvement for issue #25304, by Vincent Michel.

This commit is contained in:
Guido van Rossum 2015-10-05 16:20:00 -07:00
parent b9bf913ab3
commit 601953b679
4 changed files with 75 additions and 4 deletions

View file

@ -661,3 +661,42 @@ Task functions
.. versionchanged:: 3.4.3
If the wait is cancelled, the future *fut* is now also cancelled.
.. function:: run_coroutine_threadsafe(coro, loop)
Submit a :ref:`coroutine object <coroutine>` to a given event loop.
Return a :class:`concurrent.futures.Future` to access the result.
This function is meant to be called from a different thread than the one
where the event loop is running. Usage::
# Create a coroutine
coro = asyncio.sleep(1, result=3)
# Submit the coroutine to a given loop
future = asyncio.run_coroutine_threadsafe(coro, loop)
# Wait for the result with an optional timeout argument
assert future.result(timeout) == 3
If an exception is raised in the coroutine, the returned future will be
notified. It can also be used to cancel the task in the event loop::
try:
result = future.result(timeout)
except asyncio.TimeoutError:
print('The coroutine took too long, cancelling the task...')
future.cancel()
except Exception as exc:
print('The coroutine raised an exception: {!r}'.format(exc))
else:
print('The coroutine returned: {!r}'.format(result))
See the :ref:`concurrency and multithreading <asyncio-multithreading>`
section of the documentation.
.. note::
Unlike the functions above, :func:`run_coroutine_threadsafe` requires the
*loop* argument to be passed explicitely.
.. versionadded:: 3.4.4