Skip to content Skip to sidebar Skip to footer

Sharing Psycopg2 / Libpq Connections Across Processes

According to psycopg2 docs: libpq connections shouldn’t be used by a forked processes, so when using a module such as multiprocessing or a forking web deploy method such as Fast

Solution 1:

Your surmise is basically correct: there is no issue with a connection being opened before a fork as long as you don't attempt to use it in more than one process.

That being said, I think you misunderstood the "multiprocessing approach" link you provided. It actually demonstrates a separate connection being opened in each child. (There is a connection opened by the parent before forking, but it's not being used in any child.)

The improvement given by the answer there (versus the code in the question) was to refactor so that -- rather than opening a new connection for each task in the queue -- each child process opened a single connection and then shared it across multiple tasks executed within the same child (i.e. the connection is passed as an argument to the Task processor).

Edit: As a general practice, one should prefer creating a connection within the process that is using it. In the answer cited, a connection was being created in the parent before forking, then used in the child. This does work fine, but leaves each "child connection" open in the parent as well, which is at best a waste of resources and also a potential cause of bugs.

Post a Comment for "Sharing Psycopg2 / Libpq Connections Across Processes"