期货¶
源代码: Lib/asyncio/futures.py , Lib/asyncio/base_futures.py
Future 对象用于桥接 low-level callback-based code 使用高级异步/等待代码。
未来功能¶
- asyncio.isfuture(obj)¶
返回
True如果 obj 要么是:一个实例
asyncio.Future,一个实例
asyncio.Task,具有
_asyncio_future_blocking属性的类似未来的对象。
3.5 新版功能.
- asyncio.ensure_future(obj, *, loop=None)¶
返回:
obj*参数是这样的,如果 *obj 是一个
Future,ATask或者一个未来一样的物体 (isfuture()用于测试。)一
Task物体包裹 obj 如果 obj 是一个协同程序 (iscoroutine()用于测试);在这种情况下,协程将由ensure_future().一
Task等待的对象 obj 如果 obj 是值得期待的 (inspect.isawaitable()用于测试。)
如果 obj 以上两者都不是
TypeError提高了。重要
也见
create_task()函数,这是创建新任务的首选方法。在 3.5.1 版更改: 函数接受任何 awaitable 对象。
- asyncio.wrap_future(future, *, loop=None)¶
封装一个
concurrent.futures.Future对象中的asyncio.Future对象。
未来目标¶
- class asyncio.Future(*, loop=None)¶
未来表示异步操作的最终结果。不是线程安全的。
未来是 awaitable 对象。协程可以等待未来的对象,直到它们有结果或异常集,或者直到它们被取消。
通常,预购用于启用基于低级回调的代码(例如,在使用Asyncio实现的协议中) transports )与高级异步/等待代码进行互操作。
经验法则是永远不要在面向用户的API中公开未来的对象,建议创建未来对象的方法是调用
loop.create_future(). 这样,可选的事件循环实现可以注入自己对未来对象的优化实现。在 3.7 版更改: 增加了对
contextvars模块。- result()¶
返回未来的结果。
如果未来是 done 结果由
set_result()方法,返回结果值。如果未来是 done 并且有一个例外
set_exception()方法,此方法引发异常。如果未来 取消 ,此方法引发
CancelledError例外。如果未来的结果还不可用,则此方法将引发
InvalidStateError例外。
- set_result(result)¶
将未来标记为 done 并设置其结果。
提出一个
InvalidStateError如果未来已经存在,则出错 done .
- set_exception(exception)¶
将未来标记为 done 并设置一个例外。
提出一个
InvalidStateError如果未来已经存在,则出错 done .
- done()¶
返回
True如果未来是 done .未来是 done 如果是 取消 或者如果它有一个结果或异常集
set_result()或set_exception()调用。
- cancelled()¶
返回
True如果未来是 取消 .这种方法通常用来检查未来是否 取消 在为其设置结果或异常之前:
if not fut.cancelled(): fut.set_result(42)
- add_done_callback(callback, *, context=None)¶
添加要在将来运行的回调 done .
这个 回调 以未来对象作为其唯一参数调用。
如果未来已经是 done 当调用此方法时,将使用
loop.call_soon().仅限可选关键字 context 参数允许指定自定义
contextvars.Context对于 回调 运行进去。当否时使用当前上下文 context 提供。functools.partial()可用于向回调传递参数,例如:# Call 'print("Future:", fut)' when "fut" is done. fut.add_done_callback( functools.partial(print, "Future:"))
在 3.7 版更改: 这个 context 只添加关键字参数。见 PEP 567 了解更多详细信息。
- remove_done_callback(callback)¶
去除 回调 从回调列表中。
返回删除的回调数,通常为1,除非多次添加回调。
- cancel(msg=None)¶
取消未来并安排回调。
如果未来已经是 done 或 取消 返回
False.否则,将未来的状态更改为 取消 ,安排回调,然后返回True.在 3.9 版更改: 增加了
msg参数。
- exception()¶
返回在此将来设置的异常。
例外情况(或
None如果没有设置异常),则仅当将来 done .如果未来 取消 ,此方法引发
CancelledError例外。如果未来不是 done 然而,这种方法引发了
InvalidStateError例外。
- get_loop()¶
返回将来对象绑定到的事件循环。
3.7 新版功能.
此示例创建一个未来对象,创建并调度一个异步任务以设置未来的结果,并等待直到未来有结果:
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())
重要
未来的目标被设计成模拟 concurrent.futures.Future . 主要区别包括:
与Asyncio Futures不同,
concurrent.futures.Future无法等待实例。asyncio.Future.result()和asyncio.Future.exception()不接受 timeout 参数。asyncio.Future.result()和asyncio.Future.exception()养一个InvalidStateError当未来不是 done .已注册的回调
asyncio.Future.add_done_callback()不会立即调用。他们被安排在loop.call_soon()相反。Asyncio Future与
concurrent.futures.wait()和concurrent.futures.as_completed()功能。asyncio.Future.cancel()接受可选msg争论,但是concurrent.futures.cancel()没有。