From: Daniele Varrazzo Date: Thu, 5 Oct 2023 00:21:15 +0000 (+0200) Subject: refactor(pool): add functions to get current thread/task name X-Git-Tag: pool-3.2.0~12^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4dc8b9ec7eb05bf58167103ac6038c24b094cc0d;p=thirdparty%2Fpsycopg.git refactor(pool): add functions to get current thread/task name The latter is only possible as we dropped support for Python 3.7. --- diff --git a/psycopg_pool/psycopg_pool/_acompat.py b/psycopg_pool/psycopg_pool/_acompat.py index 620fad687..5d630ad71 100644 --- a/psycopg_pool/psycopg_pool/_acompat.py +++ b/psycopg_pool/psycopg_pool/_acompat.py @@ -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 "" + + class Queue(queue.Queue[T]): """ A Queue subclass with an interruptible get() method. diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py index ccea754ee..77fc78ee6 100644 --- a/psycopg_pool/psycopg_pool/pool.py +++ b/psycopg_pool/psycopg_pool/pool.py @@ -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: diff --git a/psycopg_pool/psycopg_pool/pool_async.py b/psycopg_pool/psycopg_pool/pool_async.py index bf3a6f5f3..c223664e5 100644 --- a/psycopg_pool/psycopg_pool/pool_async.py +++ b/psycopg_pool/psycopg_pool/pool_async.py @@ -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: