diff options
author | Guido van Rossum <guido@python.org> | 2015-10-03 15:34:34 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2015-10-03 15:34:34 (GMT) |
commit | 0d9bef927b2883d98d61dafe21428c97cee18c47 (patch) | |
tree | 943829e2241ecaec8def8cd6187da381c5740888 /Lib/asyncio/tasks.py | |
parent | 2b8cbf04df7c4e8ee8742793020df5405db8441b (diff) | |
parent | 841d9ee41a8ad0a8a372f9b84f0fa40b07bcc66b (diff) | |
download | cpython-0d9bef927b2883d98d61dafe21428c97cee18c47.zip cpython-0d9bef927b2883d98d61dafe21428c97cee18c47.tar.gz cpython-0d9bef927b2883d98d61dafe21428c97cee18c47.tar.bz2 |
Issue #25304: Add asyncio.run_coroutine_threadsafe(). By Vincent Michel. (Merge 3.4->3.5.)
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r-- | Lib/asyncio/tasks.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 434f498..5a7bd9d 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -3,7 +3,7 @@ __all__ = ['Task', 'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED', 'wait', 'wait_for', 'as_completed', 'sleep', 'async', - 'gather', 'shield', 'ensure_future', + 'gather', 'shield', 'ensure_future', 'run_coroutine_threadsafe', ] import concurrent.futures @@ -692,3 +692,19 @@ def shield(arg, *, loop=None): inner.add_done_callback(_done_callback) return outer + + +def run_coroutine_threadsafe(coro, loop): + """Submit a coroutine object to a given event loop. + + Return a concurrent.futures.Future to access the result. + """ + if not coroutines.iscoroutine(coro): + raise TypeError('A coroutine object is required') + future = concurrent.futures.Future() + + def callback(): + futures._chain_future(ensure_future(coro, loop=loop), future) + + loop.call_soon_threadsafe(callback) + return future |