我使用peewee-async(后者又使用aiomysql)查询MySQL时遇到问题
这是我的代码(我认为它与peewee-async doc中的示例几乎相同):
import asyncio
import peewee
import peewee_async
database = peewee_async.MySQLDatabase(None)
class BaseModel(peewee.Model):
id = peewee.IntegerField(primary_key=True)
class Meta:
database = database
class User(BaseModel):
name = peewee.CharField(unique=True)
password = peewee.CharField()
role = peewee.CharField()
def get_db(config, loop=None):
database.init(config['database'], user=config['user'], password=config['password'],
host=config['host'], port=config['port'])
objects = peewee_async.Manager(database, loop=loop)
objects.allow_sync = False
return objects
async def print_users(db):
users = await db.execute(User.select())
for user in users:
print(user.id, user.name)
if __name__ == '__main__':
conf = {
'database': 'db',
'user': 'user',
'password': 'secret',
'host': 'localhost',
'port': 3306
}
loop = asyncio.get_event_loop()
db = get_db(conf, loop=loop)
loop.run_until_complete(print_users(db))
现在,当我执行它时,它会查询并打印用户(在我的情况下是1个用户)。但是,它抛出异常:
(env)user@Private-006:~/project$ python tst.py
1 test
Exception ignored in: <bound method Connection.__del__ of <aiomysql.connection.Connection object at 0x7f37ce016978>>
Traceback (most recent call last):
File "/home/user/project/env/lib/python3.5/site-packages/aiomysql/connection.py", line 802, in __del__
File "/home/user/project/env/lib/python3.5/site-packages/aiomysql/connection.py", line 269, in close
File "/usr/lib/python3.5/asyncio/selector_events.py", line 566, in close
File "/usr/lib/python3.5/asyncio/base_events.py", line 497, in call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 506, in _call_soon
File "/usr/lib/python3.5/asyncio/base_events.py", line 334, in _check_closed
RuntimeError: Event loop is closed
任何帮助都将不胜感激。
答案 0 :(得分:1)
确定。似乎最终添加loop.run_until_complete(db.close())
会有所帮助。
让我们现在考虑这个问题,直到有人解释为什么我们应该明确关闭管理器,以及为什么在peewee-async文档中没有提到它。
答案 1 :(得分:0)
我遇到了同样的问题。有两种解决方案适合我,代码如下:
首先,调用数据库的close_async方法:
await database.close_async()
秒
await objects.close()
阅读源代码,您将找到objects.close()调用database.close_async()。