asyncio-Future对象
作者:互联网
Future对象
官网:https://docs.python.org/zh-cn/3/library/asyncio-future.html?highlight=future#asyncio.Future
这个例子创建一个 Future 对象,创建和调度一个异步任务去设置 Future 结果,然后等待其结果:
import asyncio async def set_after(fut, delay, value): # Sleep for *delay* seconds. await asyncio.sleep(delay) # Set *value* as a result of *fut* Future. fut.set_result(value) async def main(): # Get the current event loop. loop = asyncio.get_running_loop() # Create a new Future object. fut = loop.create_future() # Run "set_after()" coroutine in a parallel Task. # We are using the low-level "loop.create_task()" API here because # we already have a reference to the event loop at hand. # Otherwise we could have just used "asyncio.create_task()". loop.create_task( set_after(fut, 1, '... world')) print('hello ...') # Wait until *fut* has a result (1 second) and print it. print(await fut) asyncio.run(main())
标签:fut,set,对象,create,Future,loop,asyncio 来源: https://www.cnblogs.com/root0/p/15780262.html