Sqlalchemy 事件循环关闭
Sqlalchemy event loop closed
我在搞乱 sqlalchemy ORM 功能
我能够让它在我的主应用程序上运行,但是当我创建一个单独的文件 test.py
来测试某些东西时,我不断收到事件循环关闭错误:
Exception ignored in: <function Connection.__del__ at 0x7f7041c07310>
Traceback (most recent call last):
File "/home/krypt/Documents/Projects/app/env/lib/python3.9/site-packages/aiomysql/connection.py", line 1072, in __del__
File "/home/krypt/Documents/Projects/app/env/lib/python3.9/site-packages/aiomysql/connection.py", line 298, in close
File "/usr/lib/python3.9/asyncio/selector_events.py", line 700, in close
File "/usr/lib/python3.9/asyncio/base_events.py", line 746, in call_soon
File "/usr/lib/python3.9/asyncio/base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
这是 test.py
的代码:
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy.future import select
from sqlalchemy import delete
import asyncio
Base = declarative_base()
class Table(Base):
__tablename__ = 'Table'
id = Column(Integer, primary_key=True)
string = Column(String(30))
prefix = Column(String(1), default = "!")
async def main():
engine = create_async_engine("mariadb+aiomysql://user:password@127.0.0.1:3306/dbname")
session = AsyncSession(engine)
stmt = select(Table).where(Table.prefix == "!")
res = await session.execute(stmt)
row = res.scalars().first()
print(row)
asyncio.run(main())
问题似乎是 aiomysql 在事件循环关闭后试图关闭它的连接。我可以通过确保关闭会话并处理引擎来使问题中的代码工作。
async def main():
engine = create_async_engine("mariadb+aiomysql://user:password@127.0.0.1:3306/dbname")
async with AsyncSession(engine) as session:
stmt = select(Table).where(Table.prefix == "!")
res = await session.execute(stmt)
row = res.scalars().first()
print(row)
await engine.dispose()
对此有一些讨论here(接近尾声);显式关闭和处置是推荐的解决方法,以防止在事件循环关闭后执行连接的 __del__
方法。
我在搞乱 sqlalchemy ORM 功能
我能够让它在我的主应用程序上运行,但是当我创建一个单独的文件 test.py
来测试某些东西时,我不断收到事件循环关闭错误:
Exception ignored in: <function Connection.__del__ at 0x7f7041c07310>
Traceback (most recent call last):
File "/home/krypt/Documents/Projects/app/env/lib/python3.9/site-packages/aiomysql/connection.py", line 1072, in __del__
File "/home/krypt/Documents/Projects/app/env/lib/python3.9/site-packages/aiomysql/connection.py", line 298, in close
File "/usr/lib/python3.9/asyncio/selector_events.py", line 700, in close
File "/usr/lib/python3.9/asyncio/base_events.py", line 746, in call_soon
File "/usr/lib/python3.9/asyncio/base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed
这是 test.py
的代码:
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import Table, Column, Integer, String
from sqlalchemy.future import select
from sqlalchemy import delete
import asyncio
Base = declarative_base()
class Table(Base):
__tablename__ = 'Table'
id = Column(Integer, primary_key=True)
string = Column(String(30))
prefix = Column(String(1), default = "!")
async def main():
engine = create_async_engine("mariadb+aiomysql://user:password@127.0.0.1:3306/dbname")
session = AsyncSession(engine)
stmt = select(Table).where(Table.prefix == "!")
res = await session.execute(stmt)
row = res.scalars().first()
print(row)
asyncio.run(main())
问题似乎是 aiomysql 在事件循环关闭后试图关闭它的连接。我可以通过确保关闭会话并处理引擎来使问题中的代码工作。
async def main():
engine = create_async_engine("mariadb+aiomysql://user:password@127.0.0.1:3306/dbname")
async with AsyncSession(engine) as session:
stmt = select(Table).where(Table.prefix == "!")
res = await session.execute(stmt)
row = res.scalars().first()
print(row)
await engine.dispose()
对此有一些讨论here(接近尾声);显式关闭和处置是推荐的解决方法,以防止在事件循环关闭后执行连接的 __del__
方法。