我正在使用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())
client.run
方法来启动我的 Discord 机器人,但实际上我无法在已经运行的事件循环中运行它。 你有什么解决办法吗? - undefined