how to work with postgres database using coroutines

Viewed 552

We're building a backend service utilizing asyncio framework (in our case aiohttp).

We're using aiopg to make queries to the database.

Since this is one-threaded server, what is the correct implementation pattern of making a database query:

1) The server should keep just one connection, and each coroutine should simply aquire() a new cursor on that connection object?

or

2) The server should open a new connection for each coroutine?

Question about scenario (1): does it guarantee concurrency? Can the database run multiple async queries await cursor.execute() on one connection? I am pretty sure that in the non-async mode, cursors on the same connections are simply serialized. Is this different when using async? If yes, what is a max limit of cursors that can be acquired simultaneously?

Question about scenario (2): is this a valid scenario for a one-threaded asyncio server at all?

0 Answers
Related