AssertionError: yield from wasn't used with future
<p>This code:</p>

<pre><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())
</code></pre>

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

<pre><code>Traceback (most recent call last):
  File "ssh_as.py", line 20, in &lt;module&gt;
    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
</code></pre>

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

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

python python-3.x python-asyncio