Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么asyncio.wait不等待第一个\u完成_Python_Future_Python Asyncio_Event Loop_Python 3.5 - Fatal编程技术网

Python 为什么asyncio.wait不等待第一个\u完成

Python 为什么asyncio.wait不等待第一个\u完成,python,future,python-asyncio,event-loop,python-3.5,Python,Future,Python Asyncio,Event Loop,Python 3.5,我是Python3.5Asyncio新手 在我下面的代码中,asyncio.wait()不会等待stop\u future完成: import asyncio import datetime from concurrent.futures import FIRST_COMPLETED def stop(): # callback after 12 seconds print('stop', datetime.datetime.now()) stop_future.set_

我是Python3.5Asyncio新手

在我下面的代码中,asyncio.wait()不会等待stop\u future完成:

import asyncio
import datetime
from concurrent.futures import FIRST_COMPLETED


def stop():  # callback after 12 seconds

    print('stop', datetime.datetime.now())
    stop_future.set_result('Done!')


async def display_dt():

    while not stop_future.done():
        print('dt-1', datetime.datetime.now())
        # sleep 5 seconds or stop_future done
        asyncio.wait([await asyncio.sleep(5), stop_future],  return_when=FIRST_COMPLETED)
        print('dt-2', datetime.datetime.now())

    task = asyncio.Task.current_task()
    task.cancel()
    print(stop_future.result())


loop = asyncio.get_event_loop()
stop_future = asyncio.Future()
loop.call_later(12, stop)
loop.run_until_complete(display_dt())
loop.close() 
结果:

dt-1 2015-11-08 00:49:37.324582
dt-2 2015-11-08 00:49:42.325503
dt-1 2015-11-08 00:49:42.325503
dt-2 2015-11-08 00:49:47.326423
dt-1 2015-11-08 00:49:47.326423
stop 2015-11-08 00:49:49.327192  # async.wait stop_future not triggered
dt-2 2015-11-08 00:49:52.327343  # while loop finishes here
>>> Done!
dt-1 2015-11-08 01:19:06.289915
dt-2 2015-11-08 01:19:11.290836
dt-1 2015-11-08 01:19:11.290836
dt-2 2015-11-08 01:19:16.291757
dt-1 2015-11-08 01:19:16.291757
stop 2015-11-08 01:19:18.292525
dt-2 2015-11-08 01:19:18.292525  # async wait stop_future triggered
Done!
更新:

以下是更新后的功能显示的代码。现在
asyncio.wait
工作正常。
但我不明白为什么上面的代码与coro不工作

@asyncio.coroutine    # decorator necessary? It works fine without 
def display_dt():

    while not stop_future.done():
        print('dt-1', datetime.datetime.now())
        yield from asyncio.wait([asyncio.sleep(5), stop_future], return_when=FIRST_COMPLETED)
        print('dt-2', datetime.datetime.now())

    task = asyncio.Task.current_task()
    task.cancel()
    print(stop_future.result())
结果:

dt-1 2015-11-08 00:49:37.324582
dt-2 2015-11-08 00:49:42.325503
dt-1 2015-11-08 00:49:42.325503
dt-2 2015-11-08 00:49:47.326423
dt-1 2015-11-08 00:49:47.326423
stop 2015-11-08 00:49:49.327192  # async.wait stop_future not triggered
dt-2 2015-11-08 00:49:52.327343  # while loop finishes here
>>> Done!
dt-1 2015-11-08 01:19:06.289915
dt-2 2015-11-08 01:19:11.290836
dt-1 2015-11-08 01:19:11.290836
dt-2 2015-11-08 01:19:16.291757
dt-1 2015-11-08 01:19:16.291757
stop 2015-11-08 01:19:18.292525
dt-2 2015-11-08 01:19:18.292525  # async wait stop_future triggered
Done!
更新:@asyncio.coroutine#是否需要装饰器

我在以下方面找到了答案:


@asyncio.coroutine装饰程序并不神奇。事实上,如果它装饰了一个生成器函数,并且没有设置PYTHONASYNCIODEBUG环境变量,那么装饰器实际上什么也不做。为了方便框架的其他部分,它只设置了一个属性,_is_coroutine。可以将asyncio与未使用@asyncio.coroutine修饰的裸生成器一起使用。↩

您必须
等待
.wait()
协同程序,而不是
等待
您传递给
.wait()的协同程序:


而不是future.done()
或者这应该是
而不是stop\u future.done()
?好的,谢谢。我已将结果更新到您的aswer: