断言错误:未来不使用yield

时间:2016-08-29 09:35:32

标签: python python-3.x python-asyncio

此代码:

import asyncio
async def wee():
    address = 'localhost'
    port = 5432
    reader, writer = asyncio.open_connection(address, port)
    message = '/t'
    print('Send: %r' % message)
    writer.write(message.encode())

async def main():
    t2 = asyncio.ensure_future(wee())
    await t2

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

...产生错误AssertionError:yield from未用于将来 有了这个追溯:

Traceback (most recent call last):
  File "ssh_as.py", line 20, in <module>
    loop.run_until_complete(main())
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
    return future.result()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "ssh_as.py", line 16, in main
    await t2
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 358, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
    future.result()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "ssh_as.py", line 9, in wee
    reader, writer = asyncio.open_connection(address, port)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/streams.py", line 64, in open_connection
    lambda: protocol, host, port, **kwds)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/base_events.py", line 599, in create_connection
    yield from tasks.wait(fs, loop=self)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 341, in wait
    return (yield from _wait(fs, timeout, return_when, loop))
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/tasks.py", line 424, in _wait
    yield from waiter
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/futures.py", line 359, in __iter__
    assert self.done(), "yield from wasn't used with future"
AssertionError: yield from wasn't used with future

如果我只使用一个变量而不是将asyncio.open_connection解压缩到reader,writer和dummy=asyncio.open_connection(...,那么就没有这样的错误,尽管dummy对象不能用作文档的StreamReader同样 - TypeError: 'generator' object is not subscriptable。绝对不知道会发生什么,请帮助。

1 个答案:

答案 0 :(得分:4)

您需要将asyncio.open_connection(address, port)行更改为await asyncio.open_connection(address, port)。 Open connection返回future / promise等,你需要“等待”结果才能访问其内容。