From: Andrew Svetlov Date: Mon, 11 Jan 2016 12:45:25 +0000 (+0200) Subject: merge 3.4 X-Git-Tag: v3.6.0a1~799^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3af81f25057f1d40a7891dc394f94ec539c6c749;p=python merge 3.4 --- 3af81f25057f1d40a7891dc394f94ec539c6c749 diff --cc Doc/library/asyncio-task.rst index 28f3757690,76f084a267..c896b8d379 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@@ -573,15 -552,52 +575,55 @@@ Task function .. function:: iscoroutine(obj) - Return ``True`` if *obj* is a :ref:`coroutine object `. + Return ``True`` if *obj* is a :ref:`coroutine object `, + which may be based on a generator or an :keyword:`async def` coroutine. -.. function:: iscoroutinefunction(obj) +.. function:: iscoroutinefunction(func) - Return ``True`` if *func* is a decorated :ref:`coroutine function - `. + Return ``True`` if *func* is determined to be a :ref:`coroutine function + `, which may be a decorated generator function or an + :keyword:`async def` function. + .. function:: run_coroutine_threadsafe(coro, loop) + + Submit a :ref:`coroutine object ` 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 ` + section of the documentation. + + .. note:: + - Unlike the functions above, :func:`run_coroutine_threadsafe` requires the - *loop* argument to be passed explicitely. ++ Unlike other functions from the module, ++ :func:`run_coroutine_threadsafe` requires the *loop* argument to ++ be passed explicitely. + + .. versionadded:: 3.4.4, 3.5.1 + .. coroutinefunction:: sleep(delay, result=None, \*, loop=None) Create a :ref:`coroutine ` that completes after a given @@@ -685,43 -715,5 +741,3 @@@ .. 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 ` 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 ` - section of the documentation. - - .. note:: -- - Unlike the functions above, :func:`run_coroutine_threadsafe` requires the - *loop* argument to be passed explicitely. -- - .. versionadded:: 3.5.1