Preface
This article mainly introduces to you the relevant content about the asynchronous return of future objects in Python. We will share it for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.
A Future is used to represent the results to be completed in the future. Asynchronous loops can automatically complete the state triggering of such objects.
Examples are as follows:
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: all_done = () print('scheduling mark_done') event_loop.call_soon(mark_done, all_done, 'the result') print('entering event loop') result = event_loop.run_until_complete(all_done) print('returned result: {!r}'.format(result)) finally: print('closing event loop') event_loop.close() print('future result: {!r}'.format(all_done.result()))
The output result is as follows:
scheduling mark_done entering event loop setting future result to 'the result' returned result: 'the result' closing event loop future result: 'the result'
In this example, the return statement is not called, but a result can be generated. The use of Future is the same as that of coroutines.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.