Python 使用async/await打开/关闭数据库连接的最佳方法

Python 使用async/await打开/关闭数据库连接的最佳方法,python,async-await,tornado,event-loop,asyncpg,Python,Async Await,Tornado,Event Loop,Asyncpg,在我找到的教程中,每次请求都会打开和关闭连接,例如: import asyncio import asyncpg async def run(): conn = await asyncpg.connect(user='user', password='password', database='database', host='127.0.0.1') values = await conn.fetch('''SELECT

在我找到的教程中,每次请求都会打开和关闭连接,例如:

import asyncio
import asyncpg

async def run():
    conn = await asyncpg.connect(user='user', password='password',
                             database='database', host='127.0.0.1')
    values = await conn.fetch('''SELECT * FROM mytable''')
    await conn.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(run())
虽然这只适用于单个功能,但web应用程序如何

IE:例如在Tornado中,每个URL都是一个类,这导致了很多类/方法


我习惯于以阻塞方式打开连接,然后使用包装器进行异步DB调用,然后关闭连接只是为了优雅地关闭服务器,在这种情况下,使用
async/wait

而不使用asyncpg的最佳做法是什么,我假设与大多数符合asyncio的软件包一样,有一个异步上下文管理器,允许您所要求的内容

比如:

async with asyncpg.create_pool(**kwargs) as pool:
    async with pool.acquire() as connection:
        async with connection.transaction():
            result = await connection.fetchval(fetch stuff)
            connection.execute(insert stuff with result)
(摘自)

检查文档中是否提到上下文管理器或带有
async with
语句的示例,或者如果没有其他内容,则检查源代码中实现了
\uuuuuuu aenter\uuuuuu
方法的类

编辑1:

上面的例子部分取自我所联系的问题,部分是为了完整性而设计的。但为了回应您对with声明所做工作的评论:

async with asyncpg.create_pool(**kwargs) as pool:
    #in this block pool is created and open
    async with pool.acquire() as connection:
        # in this block connection is acquired and open
        async with connection.transaction():
            # in this block each executed statement is in a transaction
            execute_stuff_with_connection(connection)
        # now we are back up one logical block so the transaction is closed
        do_stuff_without_transaction_but_with_connection(connection)
    # now we are up another block and the connection is closed and returned to the pool
    do_more_stuff_with_pool(pool)
# now we are up another level and the pool is closed/exited/cleaned up
done_doing_async_stuff()

我不确定这是一个多么好的解释,也许你应该仔细阅读。

在没有使用asyncpg的情况下,我假设像在大多数符合asyncio的软件包中一样,有一个异步上下文管理器,可以完全满足你的要求

比如:

async with asyncpg.create_pool(**kwargs) as pool:
    async with pool.acquire() as connection:
        async with connection.transaction():
            result = await connection.fetchval(fetch stuff)
            connection.execute(insert stuff with result)
(摘自)

检查文档中是否提到上下文管理器或带有
async with
语句的示例,或者如果没有其他内容,则检查源代码中实现了
\uuuuuuu aenter\uuuuuu
方法的类

编辑1:

上面的例子部分取自我所联系的问题,部分是为了完整性而设计的。但为了回应您对with声明所做工作的评论:

async with asyncpg.create_pool(**kwargs) as pool:
    #in this block pool is created and open
    async with pool.acquire() as connection:
        # in this block connection is acquired and open
        async with connection.transaction():
            # in this block each executed statement is in a transaction
            execute_stuff_with_connection(connection)
        # now we are back up one logical block so the transaction is closed
        do_stuff_without_transaction_but_with_connection(connection)
    # now we are up another block and the connection is closed and returned to the pool
    do_more_stuff_with_pool(pool)
# now we are up another level and the pool is closed/exited/cleaned up
done_doing_async_stuff()

我不确定这是一个多么好的解释,也许你应该仔细阅读。

这将在最后结束它?Doe发送了
with
避免每次使用
close
,但最后总是会出现
close
?@Abdelouahab
with
语句完全用于关闭,无需指定。
async with
语句在不阻塞的情况下执行相同的操作。但是,由于我没有使用asyncpg,所以我不确定包中的哪个类/方法是实现这一点的类/方法。我在github页面的“问题”部分找到了上面的例子,所以这可能是您正在寻找的,您认为为每个请求打开和关闭DB连接是个坏主意吗?也许在打开连接时使用DB应该被阻塞?@Abdelouahab,我对这个包不太熟悉,然而,我可以想象连接池的用途是:从池中获取连接,提交事务,然后将其释放回服务器pool@Abdelouahab我还没有看到你提到的这些例子,对此我不能说。我可以说的是,连接池在使用调用
async时使用上下文管理器来管理这一点。我会试着在上面澄清。这会在最后关闭它吗?Doe发送了
with
避免每次使用
close
,但最后总是会出现
close
?@Abdelouahab
with
语句完全用于关闭,无需指定。
async with
语句在不阻塞的情况下执行相同的操作。但是,由于我没有使用asyncpg,所以我不确定包中的哪个类/方法是实现这一点的类/方法。我在github页面的“问题”部分找到了上面的例子,所以这可能是您正在寻找的,您认为为每个请求打开和关闭DB连接是个坏主意吗?也许在打开连接时使用DB应该被阻塞?@Abdelouahab,我对这个包不太熟悉,然而,我可以想象连接池的用途是:从池中获取连接,提交事务,然后将其释放回服务器pool@Abdelouahab我还没有看到你提到的这些例子,对此我不能说。我可以说的是,连接池在使用
调用
async时使用上下文管理器来管理这一点。我将尝试澄清以上内容。