Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
601953b6
Commit
601953b6
authored
Oct 05, 2015
by
Guido van Rossum
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs and one small improvement for issue #25304, by Vincent Michel.
parent
b9bf913a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
4 deletions
+75
-4
Doc/library/asyncio-dev.rst
Doc/library/asyncio-dev.rst
+9
-3
Doc/library/asyncio-task.rst
Doc/library/asyncio-task.rst
+39
-0
Lib/asyncio/tasks.py
Lib/asyncio/tasks.py
+6
-1
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_asyncio/test_tasks.py
+21
-0
No files found.
Doc/library/asyncio-dev.rst
View file @
601953b6
...
...
@@ -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.async, 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
...
...
@@ -107,6 +106,13 @@ directly its :meth:`Future.cancel` method, but::
loop.call_soon_threadsafe(fut.cancel)
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
To handle signals and to execute subprocesses, the event loop must be run in
the main thread.
...
...
Doc/library/asyncio-task.rst
View file @
601953b6
...
...
@@ -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
Lib/asyncio/tasks.py
View file @
601953b6
...
...
@@ -704,7 +704,12 @@ def run_coroutine_threadsafe(coro, loop):
future
=
concurrent
.
futures
.
Future
()
def
callback
():
futures
.
_chain_future
(
ensure_future
(
coro
,
loop
=
loop
),
future
)
try
:
futures
.
_chain_future
(
ensure_future
(
coro
,
loop
=
loop
),
future
)
except
Exception
as
exc
:
if
future
.
set_running_or_notify_cancel
():
future
.
set_exception
(
exc
)
raise
loop
.
call_soon_threadsafe
(
callback
)
return
future
Lib/test/test_asyncio/test_tasks.py
View file @
601953b6
...
...
@@ -2166,6 +2166,27 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase):
with
self
.
assertRaises
(
asyncio
.
CancelledError
):
self
.
loop
.
run_until_complete
(
future
)
def
test_run_coroutine_threadsafe_task_factory_exception
(
self
):
"""Test coroutine submission from a tread to an event loop
when the task factory raise an exception."""
# Clear the time generator
asyncio
.
ensure_future
(
self
.
add
(
1
,
2
),
loop
=
self
.
loop
)
# Schedule the target
future
=
self
.
loop
.
run_in_executor
(
None
,
self
.
target
)
# Set corrupted task factory
self
.
loop
.
set_task_factory
(
lambda
loop
,
coro
:
wrong_name
)
# Set exception handler
callback
=
test_utils
.
MockCallback
()
self
.
loop
.
set_exception_handler
(
callback
)
# Run event loop
with
self
.
assertRaises
(
NameError
)
as
exc_context
:
self
.
loop
.
run_until_complete
(
future
)
# Check exceptions
self
.
assertIn
(
'wrong_name'
,
exc_context
.
exception
.
args
[
0
])
self
.
assertEqual
(
len
(
callback
.
call_args_list
),
1
)
(
loop
,
context
),
kwargs
=
callback
.
call_args
self
.
assertEqual
(
context
[
'exception'
],
exc_context
.
exception
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment