user3292534
user3292534

Reputation:

AssertionError: yield from wasn't used with future

This code:

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())

...produces an error AssertionError: yield from wasn't used with future with this traceback:

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

If I use just one variable instead of unpacking asyncio.open_connection to reader, writer and just to dummy=asyncio.open_connection(... there is no such error, though dummy object is not usable as documentation's StreamReader as well - TypeError: 'generator' object is not subscriptable. Absolutely no idea what happens, please help.

Upvotes: 3

Views: 3092

Answers (1)

Horia Coman
Horia Coman

Reputation: 8781

You need to change the asyncio.open_connection(address, port) line to await asyncio.open_connection(address, port). Open connection returns a future/promise etc and you need to "await" that result in order to be able to access its contents.

Upvotes: 3

Related Questions