Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix async iterator protocol in aiomysql.utils._SAConnectionContextManager #410

Closed
webknjaz opened this issue Jun 2, 2019 · 7 comments · Fixed by #494
Closed

Fix async iterator protocol in aiomysql.utils._SAConnectionContextManager #410

webknjaz opened this issue Jun 2, 2019 · 7 comments · Fixed by #494

Comments

@webknjaz
Copy link
Member

webknjaz commented Jun 2, 2019

__aiter__() should be a non-coroutine method returning an instance having __anext__() coroutine method.

TypeError: 'async for' received an object from __aiter__ that does not implement __anext__: coroutine

Example: aio-libs/aiopg@ef1cabe#diff-222246bbd6dde09cb363ee2c57a67887R72

@ruiboma
Copy link

ruiboma commented Jun 24, 2019

Did the mistake arise in this way?

async def go():
	engine = await create_engine(user="xxx", password="mrb", host='localhost', db="xxx")
	conn = await engine._acquire()
	res = conn.execute("select * from xxx.user")
	async for row in res:
		print(row.id)

@webknjaz
Copy link
Member Author

I don't remember but probably yes.

@ruiboma
Copy link

ruiboma commented Aug 19, 2019

I find that this error no longer occurs, and aiter of _SAConnectionContextManager returns the ResultProxy object. ResultProxy objects already have a complete iterative protocol.

@jaggerwang
Copy link

Same problem, still there!

@MateuszCzubak
Copy link
Contributor

MateuszCzubak commented May 29, 2020

@jaggerwang Do you mind providing an example to reproduce the bug? I fail to find one since, as mentioned by @ruiboma, _SAConnectionContextManager returns the ResultProxy.

EDIT: nvm, I have managed to reproduce it myself :)

@willbuckner
Copy link

I'm seeing this here on Python 3.8:

from aiomysql import sa

app = Sanic('test app')

@app.listener('before_server_start')
async def register_db(app, loop):
    connection_params = parse_db_uri(app.config['MYSQL_URI'])

    app.pool = await sa.create_engine(**connection_params)

    async with app.pool.acquire() as connection:
        result = await connection.execute('SELECT 1')
        async for row in result.fetchall():
            print(row)
Traceback (most recent call last):
  File "./main.py", line 44, in <module>
    app.run(host='127.0.0.1', port=8000, access_log=True, debug=True)
  File "/usr/local/var/pyenv/versions/3.8.6/lib/python3.8/site-packages/sanic/app.py", line 1130, in run
    serve(**server_settings)
  File "/usr/local/var/pyenv/versions/3.8.6/lib/python3.8/site-packages/sanic/server.py", line 888, in serve
    trigger_events(before_start, loop)
  File "/usr/local/var/pyenv/versions/3.8.6/lib/python3.8/site-packages/sanic/server.py", line 696, in trigger_events
    loop.run_until_complete(result)
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "./main.py", line 33, in register_db
    async for row in result.fetchall():
TypeError: 'async for' requires an object with __aiter__ method, got coroutine
sys:1: RuntimeWarning: coroutine 'Loop.create_server' was never awaited

@fury185
Copy link

fury185 commented Feb 7, 2021

Same problem, still there
python 3.8.5

@Nothing4You Nothing4You linked a pull request Jan 30, 2022 that will close this issue
4 tasks
@Nothing4You Nothing4You added this to the 1.0 milestone Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants