Futures- – 异步I / O(Python教程)(参考资料)
期货
Future对象用于桥接基于低级回调的代码与高级异步/等待代码.
未来函数
asyncio.
isfuture
(obj)-
返回
True
如果obj是:- 的一个实例
asyncio.Future
, - 的一个实例
asyncio.Task
, - 一个带有
_asyncio_future_blocking
属性。
版本3.5中的新功能
- 的一个实例
asyncio.
ensure_future
(obj, *, loop=None)-
返回:
- obj论证,如果obj是
Future
,一个Task
,或类似Future的对象(isfuture()
用于测试。) -
Task
对象包装obj,如果obj是acoroutine(iscoroutine()
用于测试。) - 一个
Task
对象等待obj,如果obj是可以接受的(inspect.isawaitable()
用于测试。)
如果obj以上都不是
TypeError
被养了重要
另见
create_task()
功能是创建新任务的首选方式.在版本3.5.1中更改:该功能接受任何awaitable 宾语。
- obj论证,如果obj是
asyncio.
wrap_future
(future, *, loop=None)-
包裹
concurrent.futures.Future
对象asyncio.Future
object.
Future Object
- class
asyncio.
Future
(*, loop=None) -
Future表示异步操作的最终结果。不是thread-safe.
未来是等待宾语。协同程序可以等待onFuture对象,直到它们有结果或异常集,或者直到它们被取消为止
通常,Futures用于启用基于低级回调的代码(例如,在使用asyncio 传输实现的协议中)与高级异步/等待代码互操作.
经验法则是永远不要在面向用户的API中暴露Future对象,而创建Future对象的推荐方法是调用
loop.create_future()
。这样,替代事件循环实现可以为Future对象注入自己的优化实现.更改版本3.7:增加对
contextvars
module.result
()-
返回Future的结果
如果Future是done并且有
set_result()
方法设置的结果,则返回结果值.如果未来是done并且有一个由
set_exception()
方法设置的异常,这个方法引发异常.如果Future已经cancelled,这个方法引起了
CancelledError
异常。如果未来的结果还没有,这种方法会引起争议
InvalidStateError
例外。
set_result
(result)-
把未来标记为done并设定其结果.
提起
InvalidStateError
未来的错误已经是done.
set_exception
(exception)-
把未来标记为done并设置例外.
提起
InvalidStateError
未来的错误已经是done.
done
()-
返回
True
如果未来是done.未来是done如果它是 cancelled或者如果它有一个结果或一个例外设置
set_result()
或set_exception()
calls.
cancelled
()-
返回
True
如果Future是cancelled.这个方法通常用来检查Future是不是cancelled在为它设置结果或异常之前:
if not fut.cancelled(): fut.set_result(42)
add_done_callback
(callback, *, context=None)-
添加一个回调,当Future为done.
时运行callback以Future对象作为唯一参数调用
如果在调用此方法时Future已经done,则回调调度为
loop.call_soon()
.一个可选的仅关键字context参数允许指定acustom
contextvars.Context
forcallback当没有提供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)-
删除callback来自回调列表.
返回删除的回调数,通常为1,除非多次添加回调.
cancel
()-
取消未来并安排回调.
如果未来已经done或cancelled,返回
False
。否则,将Future的状态改为cancelled,安排回调,并返回True
.
exception
()-
返回在此Future上设置的异常.
例外(或
None
如果没有设置例外)只有在Future是done.时才会回复。如果未来是cancelled,这种方法会引发
CancelledError
例外如果未来不是done然而,这个方法提出了
InvalidStateError
异常
get_loop
// ()-
返回Future对象绑定的事件循环.
版本3.7.
这个例子创建一个Future对象,创建和调度异步Task来设置Future的结果,并等到Future有结果:
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())
重要
Future对象旨在模仿concurrent.futures.Future
。主要区别包括:
- 与asyncio Futures不同,
concurrent.futures.Future
实例无法等待. asyncio.Future.result()
和asyncio.Future.exception()
不接受timeout论点。asyncio.Future.result()
和asyncio.Future.exception()
当Future不是InvalidStateError
时,如果done.- 注册了回调,则不会立即调用
asyncio.Future.add_done_callback()
异常。它们安排在loop.call_soon()
而不是 - asyncio未来与
concurrent.futures.wait()
和concurrent.futures.as_completed()
functions.
文章导航
- 未来的功能
- 未来的对象
超哥软件库 » Futures- – 异步I / O(Python教程)(参考资料)