Last active
December 22, 2024 22:45
-
-
Save jeorgen/4eea9b9211bafeb18ada to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?