aio-libs / aio-libs/aiomysql

connection.cursor() result is not a valid coroutine

未關閉
#543 0 則留言 7 個 reaction 已指派 0 人 在 GitHub 檢視
sqlalchemy
主要語言
Python
星號
1.9k
分支
272
PR 合併指標
30 天內沒有已合併 PR

描述

hey there -

For context, as you might be aware, SQLAlchemy now has native asyncio support: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html

In order to achieve this, we are doing a little work with greenlet and asyncio under the hood to bridge the gap through SQLAlchemy's internals. We have complete support for the asyncpg database driver for example. aiomysql is the obvious choice for us to also add MySQL support.

Things are mostly good but the current issue we are running into is the aiomysql _ContextManager utility which does not fully act like a coroutine when a Future object is placed into it, which seems to be what connection.cursor() is doing. We need everything marked as a "coroutine" to run in methods like asyncio.run_until_complete(). That's not what we normally use for the asyncio support but within our test suite we use it to adapt an asyncio driver to sync so that all of our normal test cases for datatypes and such can be exercised.

Below is a script illustrating the issue.

```python
import asyncio

import aiomysql

loop = asyncio.get_event_loop()

coroutine = aiomysql.connect(
host="127.0.0.1", port=3306, user="root", password="", db="test"
)

# aiomysql.connect() is a coroutine: OK
conn = loop.run_until_complete(coroutine)

coroutine = conn.autocommit(True)

# aiomysql.connection.autocommit() is a coroutine: OK
loop.run_until_complete(coroutine)

coroutine = conn.cursor()

# aiomysql.connection.cursor() is a coroutine: nope
# AttributeError: '_asyncio.Future' object has no attribute 'send'
cursor = loop.run_until_complete(coroutine)

```

stack trace:

```
Traceback (most recent call last):
File "test4.py", line 23, in
cursor = loop.run_until_complete(coroutine)
File "/opt/python-3.8.3/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/home/classic/.venv3/lib/python3.8/site-packages/aiomysql/utils.py", line 13, in send
return self._coro.send(value)
AttributeError: '_asyncio.Future' object has no attribute 'send'

```

thanks for aiomysql!

cc @caselit

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。