]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Don't use a worker in pool.putconn() if not needed
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 11 Mar 2021 14:31:55 +0000 (15:31 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
If _reset is not set there is little work to do so the overhead of a
context switch is measurable and not necessary.

psycopg3/psycopg3/pool/async_pool.py
psycopg3/psycopg3/pool/pool.py

index 451033a2b632cc84b1dc99389398d3496f7acc47..4233938b070f635d00ee5959f3ae3530a0fc6b20 100644 (file)
@@ -217,7 +217,10 @@ class AsyncConnectionPool(BasePool[AsyncConnection]):
             return
 
         # Use a worker to perform eventual maintenance work in a separate thread
-        self.run_task(ReturnConnection(self, conn))
+        if self._reset:
+            self.run_task(ReturnConnection(self, conn))
+        else:
+            await self._return_connection(conn)
 
     async def close(self, timeout: float = 5.0) -> None:
         """Close the pool and make it unavailable to new clients.
index 741c57bc8cc9643703a2956332f2959036e4d27b..12604846ec53bee638756b2a7dbecefd5cd674fd 100644 (file)
@@ -228,7 +228,10 @@ class ConnectionPool(BasePool[Connection]):
             return
 
         # Use a worker to perform eventual maintenance work in a separate thread
-        self.run_task(ReturnConnection(self, conn))
+        if self._reset:
+            self.run_task(ReturnConnection(self, conn))
+        else:
+            self._return_connection(conn)
 
     def close(self, timeout: float = 1.0) -> None:
         """Close the pool and make it unavailable to new clients.