From d27022fe5fc49292e49f45d98c1c9cf0b76f1454 Mon Sep 17 00:00:00 2001 From: fjetter Date: Wed, 16 Apr 2025 11:42:36 +0200 Subject: [PATCH 1/3] Cache WorkerState.host --- distributed/scheduler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distributed/scheduler.py b/distributed/scheduler.py index a026e897ed6..a743cada0e3 100644 --- a/distributed/scheduler.py +++ b/distributed/scheduler.py @@ -31,7 +31,7 @@ Set, ) from contextlib import suppress -from functools import partial +from functools import cached_property, partial from typing import TYPE_CHECKING, Any, ClassVar, Literal, NamedTuple, cast, overload import psutil @@ -584,7 +584,7 @@ def has_what(self) -> Set[TaskState]: """ return self._has_what.keys() - @property + @cached_property def host(self) -> str: return get_address_host(self.address) From d084e58eefcd354f293eed83238f8aff194c3f5a Mon Sep 17 00:00:00 2001 From: fjetter Date: Wed, 16 Apr 2025 12:58:10 +0200 Subject: [PATCH 2/3] as attribute --- distributed/scheduler.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/distributed/scheduler.py b/distributed/scheduler.py index a743cada0e3..b0922b5451e 100644 --- a/distributed/scheduler.py +++ b/distributed/scheduler.py @@ -31,7 +31,7 @@ Set, ) from contextlib import suppress -from functools import cached_property, partial +from functools import partial from typing import TYPE_CHECKING, Any, ClassVar, Literal, NamedTuple, cast, overload import psutil @@ -513,6 +513,8 @@ class WorkerState: #: on the worker. needs_what: dict[TaskState, int] + host: str + __slots__ = tuple(__annotations__) def __init__( @@ -564,6 +566,7 @@ def __init__( self.needs_what = {} self._network_occ = 0 self._occupancy_cache = None + self.host = get_address_host(self.address) def __hash__(self) -> int: return self._hash @@ -584,10 +587,6 @@ def has_what(self) -> Set[TaskState]: """ return self._has_what.keys() - @cached_property - def host(self) -> str: - return get_address_host(self.address) - @property def memory(self) -> MemoryState: """Polished memory metrics for the worker. From 53fad0169e25debe31d54dcfbb09957d55b8648f Mon Sep 17 00:00:00 2001 From: fjetter Date: Wed, 16 Apr 2025 14:03:25 +0200 Subject: [PATCH 3/3] final hints --- distributed/scheduler.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/distributed/scheduler.py b/distributed/scheduler.py index b0922b5451e..225a11ee86e 100644 --- a/distributed/scheduler.py +++ b/distributed/scheduler.py @@ -32,7 +32,16 @@ ) from contextlib import suppress from functools import partial -from typing import TYPE_CHECKING, Any, ClassVar, Literal, NamedTuple, cast, overload +from typing import ( + TYPE_CHECKING, + Any, + ClassVar, + Final, + Literal, + NamedTuple, + cast, + overload, +) import psutil import tornado.web @@ -413,7 +422,7 @@ class WorkerState: #: This worker's unique key. This can be its connected address #: (such as ``"tcp://127.0.0.1:8891"``) or an alias (such as ``"alice"``). - address: str + address: Final[str] pid: int name: Hashable @@ -513,7 +522,8 @@ class WorkerState: #: on the worker. needs_what: dict[TaskState, int] - host: str + #: The hostname / IP address identifying the machine this worker is on. + host: Final[str] __slots__ = tuple(__annotations__)