Futures- – 异步I / O(Python教程)(参考资料)

期货

Future对象用于桥接基于低级回调的代码与高级异步/等待代码.

未来函数
asyncio.isfutureobj

返回True如果obj是:

  • 的一个实例asyncio.Future,
  • 的一个实例asyncio.Task,
  • 一个带有_asyncio_future_blocking属性。

版本3.5中的新功能

asyncio.ensure_futureobj, *, loop=None

返回:

  • obj论证,如果objFuture,一个 Task,或类似Future的对象(isfuture()用于测试。)
  • Task对象包装obj,如果obj是acoroutine(iscoroutine()用于测试。)
  • 一个Task对象等待obj,如果obj是可以接受的(inspect.isawaitable()用于测试。)

如果obj以上都不是TypeError被养了

重要

另见create_task()功能是创建新任务的首选方式.

在版本3.5.1中更改:该功能接受任何awaitable 宾语。

asyncio.wrap_futurefuture, *, loop=None

包裹concurrent.futures.Future对象asyncio.Futureobject.

Future Object
class asyncio.Future*, loop=None

Future表示异步操作的最终结果。不是thread-safe.

未来是等待宾语。协同程序可以等待onFuture对象,直到它们有结果或异常集,或者直到它们被取消为止

通常,Futures用于启用基于低级回调的代码(例如,在使用asyncio 传输实现的协议中)与高级异步/等待代码互操作.

经验法则是永远不要在面向用户的API中暴露Future对象,而创建Future对象的推荐方法是调用loop.create_future()。这样,替代事件循环实现可以为Future对象注入自己的优化实现.

更改版本3.7:增加对contextvarsmodule.

result()

返回Future的结果

如果Future是done并且有set_result()方法设置的结果,则返回结果值.

如果未来是done并且有一个由set_exception()方法设置的异常,这个方法引发异常.

如果Future已经cancelled,这个方法引起了CancelledError异常。

如果未来的结果还没有,这种方法会引起争议InvalidStateError例外。

set_resultresult

把未来标记为done并设定其结果.

提起InvalidStateError未来的错误已经是done.

set_exceptionexception

把未来标记为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_callbackcallback, *, 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()

取消未来并安排回调.

如果未来已经donecancelled,返回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.

文章导航

  • 未来的功能
  • 未来的对象
本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。
超哥软件库 » Futures- – 异步I / O(Python教程)(参考资料)