mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Docs and one small improvement for issue #25304, by Vincent Michel. (Merge 3.4->3.5.)
This commit is contained in:
commit
5db034acfa
4 changed files with 75 additions and 4 deletions
|
@ -96,10 +96,9 @@ the same thread. But when the task uses ``yield from``, the task is suspended
|
|||
and the event loop executes the next task.
|
||||
|
||||
To schedule a callback from a different thread, the
|
||||
:meth:`BaseEventLoop.call_soon_threadsafe` method should be used. Example to
|
||||
schedule a coroutine from a different thread::
|
||||
:meth:`BaseEventLoop.call_soon_threadsafe` method should be used. Example::
|
||||
|
||||
loop.call_soon_threadsafe(asyncio.ensure_future, coro_func())
|
||||
loop.call_soon_threadsafe(callback, *args)
|
||||
|
||||
Most asyncio objects are not thread safe. You should only worry if you access
|
||||
objects outside the event loop. For example, to cancel a future, don't call
|
||||
|
@ -110,6 +109,13 @@ directly its :meth:`Future.cancel` method, but::
|
|||
To handle signals and to execute subprocesses, the event loop must be run in
|
||||
the main thread.
|
||||
|
||||
To schedule a coroutine object from a different thread, the
|
||||
:func:`run_coroutine_threadsafe` function should be used. It returns a
|
||||
:class:`concurrent.futures.Future` to access the result::
|
||||
|
||||
future = asyncio.run_coroutine_threadsafe(coro_func(), loop)
|
||||
result = future.result(timeout) # Wait for the result with a timeout
|
||||
|
||||
The :meth:`BaseEventLoop.run_in_executor` method can be used with a thread pool
|
||||
executor to execute a callback in different thread to not block the thread of
|
||||
the event loop.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue