使用Python访问数据库时,操作过程中连接被关闭。

11

我在Heroku上设置了一个PostgreSQL服务器,除此之外还有我的Python机器人也在Heroku上运行,但是机器人无法连接到数据库。

我确保密码、用户名等都是正确的。

这是连接使用的方法:

async def create_db_pool():
    bot.pg_con = await asyncpg.create_pool(database="dbname", 
    user="username", 
    password="dbpw")

这是我如何运行它的方式:

bot.loop.run_until_complete(create_db_pool())

预期可以访问数据库并读写数据,但是我收到了以下错误:

asyncpg.exceptions.ConnectionDoesNotExistError: connection was closed in the middle of operation
Task was destroyed but it is pending!
task: <Task pending coro=<chng_pr() running at I:/Python/HardCoreDisBot/Commands.py:38> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000002571E9B1978>()]>>

我不确定你所说的“中间关闭”错误是什么,但你不应该在代码中硬编码数据库凭据。敏感信息不应该出现在你的代码中。在Heroku上,你可以将一个数据库附加到多个应用程序,并像往常一样使用环境变量进行连接。点击此处了解更多信息。 - Chris
@Chris 抱歉,我不知道如何做这个。 - Akira
从我的先前评论中的链接开始,阅读您在那里找到的内容。 - Chris
1
代码看起来还不错(到目前为止),你需要展示更多一点(如何创建bot,运行哪个SQL,关闭连接)。 - Beppe C
你可以使用线程锁,每个线程在开始执行时获取锁,在完成后释放锁。 - Omar Kamoon
2个回答

2

我看了一下你的问题,并发现一个类似的线程,他们似乎通过在代码中添加max_inactive_connection_lifetime来找到了解决方法。 这里是该线程的链接。

async def create_db_pool():
    bot.pg_con = await asyncpg.create_pool(database="dbname", 
    user="username", 
    password="dbpw",
    max_inactive_connection_lifetime=3)

0

我和Starlette有相同的问题。

据我所知,这个问题的原因是asyncpg没有共享相同的异步循环。

我遵循了asyncpg.create_pool(..., loop=asyncio.get_event_loop())的建议,虽然我还没有完全解决这个问题,但至少在刷新后连接得到了重新建立。

从我撰写此文时看来,这似乎仍然是一个活跃的问题: https://github.com/MagicStack/asyncpg/issues/309

Sanic用户面临了类似的情况 https://github.com/sanic-org/sanic/issues/152

编辑

对我来说,目前这个方法有效:https://magicstack.github.io/asyncpg/current/usage.html#connection-pools

async def handle(request):
"""Handle incoming requests."""
pool = request.app['pool']
power = int(request.match_info.get('power', 10))

# Take a connection from the pool.
async with pool.acquire() as connection:
    # Open a transaction.
    async with connection.transaction():
        # Run the query passing the request argument.
        result = await connection.fetchval('select 2 ^ $1', power)
        return web.Response(
            text="2 ^ {} is {}".format(power, result))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接