Skip to content

Instantly share code, notes, and snippets.

@jeorgen
Last active December 22, 2024 22:45
Show Gist options
  • Select an option

  • Save jeorgen/4eea9b9211bafeb18ada to your computer and use it in GitHub Desktop.

Select an option

Save jeorgen/4eea9b9211bafeb18ada to your computer and use it in GitHub Desktop.
import os
from psycopg2.pool import ThreadedConnectionPool
class ProcessSafePoolManager:
def __init__(self, *args, **kwargs):
self.last_seen_process_id = os.getpid()
self.args = args
self.kwargs = kwargs
self._init()
def _init(self):
self._pool = ThreadedConnectionPool(*self.args, **self.kwargs)
def getconn(self):
current_pid = os.getpid()
if not (current_pid == self.last_seen_process_id):
self._init()
print "New id is %s, old id was %s" % (current_pid, self.last_seen_process_id)
self.last_seen_process_id = current_pid
return self._pool.getconn()
def putconn(self, conn):
return self._pool.putconn(conn)
pool = ProcessSafePoolManager(1, 10, "host='127.0.0.1' port=12099")
@SirZach
Copy link

SirZach commented Jun 7, 2018

Thanks for posting this! I'm trying to use this myself and I'm getting An unexpected error occurred: trying to put unkeyed connection. Did you encounter the same at any point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment