]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(pool): add functions to get current thread/task name
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 5 Oct 2023 00:21:15 +0000 (02:21 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 11 Oct 2023 21:45:38 +0000 (23:45 +0200)
The latter is only possible as we dropped support for Python 3.7.

psycopg_pool/psycopg_pool/_acompat.py
psycopg_pool/psycopg_pool/pool.py
psycopg_pool/psycopg_pool/pool_async.py

index 620fad687bc3d787cd20fb71ef7843e98257c172..5d630ad71da674cc2902e59b45555dd75ae0b1c3 100644 (file)
@@ -24,6 +24,15 @@ Lock = threading.RLock
 ALock = asyncio.Lock
 
 
+def current_thread_name() -> str:
+    return threading.current_thread().name
+
+
+def current_task_name() -> str:
+    t = asyncio.current_task()
+    return t.get_name() if t else "<no task>"
+
+
 class Queue(queue.Queue[T]):
     """
     A Queue subclass with an interruptible get() method.
index ccea754eec572ee0e1c925d661d006b8363b9af1..77fc78ee647d2e2520057eb52bfce257564d1e1c 100644 (file)
@@ -25,6 +25,7 @@ from .sched import Scheduler
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
 from ._compat import Deque
 from ._acompat import Condition, Event, Lock, Queue, spawn, gather
+from ._acompat import current_thread_name
 
 logger = logging.getLogger("psycopg.pool")
 
@@ -508,9 +509,7 @@ class ConnectionPool(Generic[CT], BasePool):
             task = q.get()
 
             if isinstance(task, StopWorker):
-                logger.debug(
-                    "terminating working thread %s", threading.current_thread().name
-                )
+                logger.debug("terminating working thread %s", current_thread_name())
                 return
 
             # Run the task. Make sure don't die in the attempt.
@@ -824,7 +823,7 @@ class MaintenanceTask(ABC):
             logger.debug("task run discarded: %s", self)
             return
 
-        logger.debug("task running in %s: %s", threading.current_thread().name, self)
+        logger.debug("task running in %s: %s", current_thread_name(), self)
         self._run(pool)
 
     def tick(self) -> None:
index bf3a6f5f3db394c6ff983a2ec397bf60965c84e8..c223664e50ddd356ce0536603e7e9787f85e716e 100644 (file)
@@ -25,6 +25,7 @@ from .base import ConnectionAttempt, BasePool
 from .errors import PoolClosed, PoolTimeout, TooManyRequests
 from ._compat import Deque
 from ._acompat import ACondition, AEvent, ALock, AQueue, aspawn, agather
+from ._acompat import current_task_name
 from .sched_async import AsyncScheduler
 
 logger = logging.getLogger("psycopg.pool")
@@ -522,7 +523,7 @@ class AsyncConnectionPool(Generic[ACT], BasePool):
             task = await q.get()
 
             if isinstance(task, StopWorker):
-                logger.debug("terminating working task")
+                logger.debug("terminating working task %s", current_task_name())
                 return
 
             # Run the task. Make sure don't die in the attempt.
@@ -838,6 +839,7 @@ class MaintenanceTask(ABC):
             logger.debug("task run discarded: %s", self)
             return
 
+        logger.debug("task running in %s: %s", current_task_name(), self)
         await self._run(pool)
 
     async def tick(self) -> None: