Skip to content

Commit 3f084e5

Browse files
committed
⚡ perf: use psycopg2.pool
1 parent d89aaed commit 3f084e5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

transpose/core/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def __init__(
2323

2424
# connect to the database
2525
try:
26-
db = psycopg2.connect(
26+
pool = psycopg2.pool.ThreadedConnectionPool(
27+
1,
28+
20,
2729
host=host,
2830
port=port,
2931
user=user,
@@ -38,7 +40,7 @@ def __init__(
3840
self.logger = logging.getLogger(__name__)
3941

4042
# update classes
41-
self.db = db
43+
self.__pool = pool
4244
self.sync = SyncClient(self)
4345

4446
def _execute(self, query: str, args: tuple = ()) -> list[dict]:
@@ -50,9 +52,12 @@ def _execute(self, query: str, args: tuple = ()) -> list[dict]:
5052
"""
5153

5254
try:
53-
with self.db.cursor(cursor_factory=RealDictCursor) as cursor:
55+
db = self.__pool.getconn()
56+
with db.cursor(cursor_factory=RealDictCursor) as cursor:
5457
cursor.execute(query, args)
55-
return cursor.fetchall()
58+
response = cursor.fetchall()
59+
self.__pool.putconn(db)
60+
return response
5661
except KeyboardInterrupt:
5762
sys.exit()
5863

0 commit comments

Comments
 (0)