zoukankan      html  css  js  c++  java
  • asyncioFuture对象

    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())
  • 相关阅读:
    JAVA EE企业级开发四步走
    区间dp笔记√
    TYVJ P1016 装箱问题
    树状数组的笔记√(hzwer blog)
    忠诚//线段树
    线段树笔记√
    P1005 采药
    超级书架【未完】
    P1082 找朋友
    数字三角形系列
  • 原文地址:https://www.cnblogs.com/root0/p/15780262.html
Copyright © 2011-2022 走看看