I was reading the Python documentation and PyMotW book trying to learn Async/Await, Futures, and Tasks.
Coroutines and Tasks documentation:
Normally there is no need to create Future objects at the application level code.
From the future documentation it states the following:
loop.create_future()
Create an asyncio.Future object attached to the event loop.
This is the preferred way to create Futures in asyncio. This lets third-party event loops provide alternative implementations of the Future object (with better performance or instrumentation).
However, in PyMotW chapter on Future, the author creates a future object like this:
all_done = asyncio.Future() I assume because the book is slightly behind the current version of Python. To correct this, I did the following:
future_Obj = event_loop.create_future() So the authors full code becomes:
import asyncio def mark_done(future, result): print('setting future result to {!r}'.format(result)) future.set_result(result) event_loop = asyncio.get_event_loop() try: future_Obj = event_loop.create_future() print('scheduling mark_done') event_loop.call_soon(mark_done, future_Obj, 'the result') print('entering event loop') result = event_loop.run_until_complete(future_Obj) print('returned result: {!r}'.format(result)) finally: print('closing event loop') event_loop.close() print('future result: {!r}'.format(future_Obj.result())) Question:
Is future_Obj = event_loop.create_future() in the sample above, the correct way to create a future object according to the documentation?