2

Psycopg3 supports async connections and async cursors. I have multiple writer tasks writing to a database. Should I pass each of those tasks the same AsyncConnection? If not, should I pass them the same AsyncCursor? Or should I make a new connection in each of the functions?

eg: below, would I send aconn to each of my tasks? Or cur? Or should this code be in each of the tasks?

async with await psycopg.AsyncConnection.connect() as aconn:
    async with aconn.cursor() as cur:
        await cur.execute(...)

All of the example code I could find had only one task being created. Thanks!

  • 1
    I cannot give you a good answer since I have not tried it, but have you considered using psycopg3's connection pooling? https://www.psycopg.org/psycopg3/docs/advanced/pool.html#connection-pools – Mike Organek Jun 24 '22 at 20:28
  • I considered it but it appears to not support async connections - just synchronous ones - so it wouldn't return control to the event loop during the writing process – Justin Calareso Jun 25 '22 at 12:16
  • The docs say there is an async connection pool implementation: https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.AsyncConnectionPool – Mike Organek Jun 25 '22 at 18:35

0 Answers0