Commit d7e19bb5 authored by Yury Selivanov's avatar Yury Selivanov

docs/asyncio: Document new ensure_future() and deprecated async()

parent 71854618
...@@ -210,7 +210,7 @@ Example of unhandled exception:: ...@@ -210,7 +210,7 @@ Example of unhandled exception::
raise Exception("not consumed") raise Exception("not consumed")
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio.async(bug()) asyncio.ensure_future(bug())
loop.run_forever() loop.run_forever()
loop.close() loop.close()
...@@ -234,7 +234,7 @@ traceback where the task was created. Output in debug mode:: ...@@ -234,7 +234,7 @@ traceback where the task was created. Output in debug mode::
future: <Task finished coro=<bug() done, defined at test.py:3> exception=Exception('not consumed',) created at test.py:8> future: <Task finished coro=<bug() done, defined at test.py:3> exception=Exception('not consumed',) created at test.py:8>
source_traceback: Object created at (most recent call last): source_traceback: Object created at (most recent call last):
File "test.py", line 8, in <module> File "test.py", line 8, in <module>
asyncio.async(bug()) asyncio.ensure_future(bug())
Traceback (most recent call last): Traceback (most recent call last):
File "asyncio/tasks.py", line 237, in _step File "asyncio/tasks.py", line 237, in _step
result = next(coro) result = next(coro)
...@@ -257,14 +257,14 @@ coroutine in another coroutine and use classic try/except:: ...@@ -257,14 +257,14 @@ coroutine in another coroutine and use classic try/except::
print("exception consumed") print("exception consumed")
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio.async(handle_exception()) asyncio.ensure_future(handle_exception())
loop.run_forever() loop.run_forever()
loop.close() loop.close()
Another option is to use the :meth:`BaseEventLoop.run_until_complete` Another option is to use the :meth:`BaseEventLoop.run_until_complete`
function:: function::
task = asyncio.async(bug()) task = asyncio.ensure_future(bug())
try: try:
loop.run_until_complete(task) loop.run_until_complete(task)
except Exception: except Exception:
...@@ -303,14 +303,14 @@ operations:: ...@@ -303,14 +303,14 @@ operations::
@asyncio.coroutine @asyncio.coroutine
def test(): def test():
asyncio.async(create()) asyncio.ensure_future(create())
asyncio.async(write()) asyncio.ensure_future(write())
asyncio.async(close()) asyncio.ensure_future(close())
yield from asyncio.sleep(2.0) yield from asyncio.sleep(2.0)
loop.stop() loop.stop()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
asyncio.async(test()) asyncio.ensure_future(test())
loop.run_forever() loop.run_forever()
print("Pending tasks at exit: %s" % asyncio.Task.all_tasks(loop)) print("Pending tasks at exit: %s" % asyncio.Task.all_tasks(loop))
loop.close() loop.close()
...@@ -338,13 +338,13 @@ To fix the example, tasks must be marked with ``yield from``:: ...@@ -338,13 +338,13 @@ To fix the example, tasks must be marked with ``yield from``::
@asyncio.coroutine @asyncio.coroutine
def test(): def test():
yield from asyncio.async(create()) yield from asyncio.ensure_future(create())
yield from asyncio.async(write()) yield from asyncio.ensure_future(write())
yield from asyncio.async(close()) yield from asyncio.ensure_future(close())
yield from asyncio.sleep(2.0) yield from asyncio.sleep(2.0)
loop.stop() loop.stop()
Or without ``asyncio.async()``:: Or without ``asyncio.ensure_future()``::
@asyncio.coroutine @asyncio.coroutine
def test(): def test():
...@@ -374,7 +374,7 @@ traceback where the task was created. Example of log in debug mode:: ...@@ -374,7 +374,7 @@ traceback where the task was created. Example of log in debug mode::
Task was destroyed but it is pending! Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last): source_traceback: Object created at (most recent call last):
File "test.py", line 15, in <module> File "test.py", line 15, in <module>
task = asyncio.async(coro, loop=loop) task = asyncio.ensure_future(coro, loop=loop)
task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()] created at test.py:7> created at test.py:15> task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()] created at test.py:7> created at test.py:15>
......
...@@ -364,7 +364,7 @@ Simple example querying HTTP headers of the URL passed on the command line:: ...@@ -364,7 +364,7 @@ Simple example querying HTTP headers of the URL passed on the command line::
url = sys.argv[1] url = sys.argv[1]
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
task = asyncio.async(print_http_headers(url)) task = asyncio.ensure_future(print_http_headers(url))
loop.run_until_complete(task) loop.run_until_complete(task)
loop.close() loop.close()
......
...@@ -296,7 +296,7 @@ Example combining a :class:`Future` and a :ref:`coroutine function ...@@ -296,7 +296,7 @@ Example combining a :class:`Future` and a :ref:`coroutine function
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
future = asyncio.Future() future = asyncio.Future()
asyncio.async(slow_operation(future)) asyncio.ensure_future(slow_operation(future))
loop.run_until_complete(future) loop.run_until_complete(future)
print(future.result()) print(future.result())
loop.close() loop.close()
...@@ -332,7 +332,7 @@ flow:: ...@@ -332,7 +332,7 @@ flow::
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
future = asyncio.Future() future = asyncio.Future()
asyncio.async(slow_operation(future)) asyncio.ensure_future(slow_operation(future))
future.add_done_callback(got_result) future.add_done_callback(got_result)
try: try:
loop.run_forever() loop.run_forever()
...@@ -461,9 +461,9 @@ Example executing 3 tasks (A, B, C) in parallel:: ...@@ -461,9 +461,9 @@ Example executing 3 tasks (A, B, C) in parallel::
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
tasks = [ tasks = [
asyncio.async(factorial("A", 2)), asyncio.ensure_future(factorial("A", 2)),
asyncio.async(factorial("B", 3)), asyncio.ensure_future(factorial("B", 3)),
asyncio.async(factorial("C", 4))] asyncio.ensure_future(factorial("C", 4))]
loop.run_until_complete(asyncio.wait(tasks)) loop.run_until_complete(asyncio.wait(tasks))
loop.close() loop.close()
...@@ -510,17 +510,25 @@ Task functions ...@@ -510,17 +510,25 @@ Task functions
The futures ``f`` are not necessarily members of fs. The futures ``f`` are not necessarily members of fs.
.. function:: async(coro_or_future, \*, loop=None) .. function:: ensure_future(coro_or_future, \*, loop=None)
Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in Schedule the execution of a :ref:`coroutine object <coroutine>`: wrap it in
a future. Return a :class:`Task` object. a future. Return a :class:`Task` object.
If the argument is a :class:`Future`, it is returned directly. If the argument is a :class:`Future`, it is returned directly.
.. versionadded:: 3.4.4
.. seealso:: .. seealso::
The :meth:`BaseEventLoop.create_task` method. The :meth:`BaseEventLoop.create_task` method.
.. function:: async(coro_or_future, \*, loop=None)
A deprecated alias to :func:`ensure_future`.
.. deprecated:: 3.4.4
.. function:: gather(\*coros_or_futures, loop=None, return_exceptions=False) .. function:: gather(\*coros_or_futures, loop=None, return_exceptions=False)
Return a future aggregating results from the given coroutine objects or Return a future aggregating results from the given coroutine objects or
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment