2

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?

PawelRoman
  • 6,122
  • 5
  • 30
  • 40
  • Without knowing more about your application's architecture it's a bit hard to give specific help, but you probably want to use a connection pool. The [aiopg docs](https://aiopg.readthedocs.io/en/stable/#basics) include an example of using a pool. – dirn Jun 25 '19 at 14:37
  • I am using conn pool. The question is rather simple: when having multiple concurrent users (multiple coroutines making queries to the DB simultaneously) should each coroutine open a new connection or just new cursor from shared connection? – PawelRoman Jun 26 '19 at 08:10
  • If you are using a pool, all of your connections are already shared, so getting a new one or reusing an old one are effectively the same thing. – dirn Jun 26 '19 at 10:49

0 Answers0