Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
context-managed resource on module level

I am looking for a pattern where I have multiple functions that need access to a resource that is context-managed.

In particular, I am using fastAPI and want to re-use aiopg (async psycopg2) connections.

This is the basic layout:

@app.get("/hello")
def hello():
    async with aiopg.connect(...) as conn:
        async with conn.cursor(...):
            return cursor.execute(...)

Now I want to avoid a connection per route. I could think of an object outside, and in the route I either access the conn property or await creation (and store back) and then just use the with on the cursor() method.

class Pg():
    async def conn(self):
        if not self._conn:
            self._conn = await aiopg.connect(...)
        return self._conn

myPg = Pg()

@app.get("/hello")
def hello():
    conn = await myPg.conn()
    async with conn.cursor(...):
        return cursor.execute(...)

However, I then would lose the ability to automatically close the connection.

I think that I miss something really obvious here and hope that someone could guide me how to implement this properly.

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

aiopg provides a Pool class that can manage connections.

just create a pool instance at the module level:

pool = await aiopg.create_pool('<your connection string>')

Then you can use the Pool.acquire context-manager to get a connection:

async with pool.acquire() as conn:
    ...

If connections already exist in the pool, they will be re-used.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda