Skip to content
Advertisement

Sharing a postgres connection pool between python multiproccess

I am trying to use psycopg2’s connection pool with python’s multiprocess library.

Currently, attempting to share the connection pool amongst threads in the manner described above causes:

JavaScript

The following code should reproduce the error, which the caveat that the reader has to set up a simple postgres database.

JavaScript

What I have already tried:

  1. Having each process open and close its own connection to the database, instead of attempting to use a connection pool. This is slow.
  2. Having each process use its own connection pool, this is also slow.
  3. Passing a connection a psycopg2 connection object to each process, instead of having this implicitly called with the with statement in the sql query. This throws an error claiming that the connection object is not pickle-able.

Note: If I put a sleep operation in all but one of the processes, the non-sleeping processes runs fine and executes its query, until the remaining threads un-sleep, then I get the above error.

What I have already read:

  1. Share connection to postgres db across processes in Python
  2. Python: decryption failed or bad record mac when calling from Thread
  3. Connection problems with SQLAlchemy and multiple processes

Finally:

How can I use a connection pool (psycopg2) with python’s multiprocess (multiprocessing). I am open to using other libraries so long as they work with python and postgresql databases.

Advertisement

Answer

Here is my solution. The solution can be stated in 2 parts:

  1. Have a wrapper function that will be executed by each unique Process. The main purpose of this wrapper function is to create its own connection pool
  2. For each query executed by the wrapper function in Step 1, pass the connection pool to the query function (in the example above, this is test_query)

In more detail with reference to the example in the question:

Step 1

Create the wrapper function that will be re-using one connection pool per Process:

JavaScript

Step 2

Modify the query function to accept a connection pool:

Old test_query:

JavaScript

New test_query:

JavaScript
Advertisement