Skip to content

Commit

Permalink
Remove metaclass magic
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky committed Mar 14, 2022
1 parent 94acdc5 commit 297bed6
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions distributed/worker_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,17 @@ class SendMessageToScheduler(Instruction):
#: Matches a key in Scheduler.stream_handlers
op: ClassVar[str]

def __init_subclass__(cls, op: str):
cls.op = op

def to_dict(self) -> dict[str, Any]:
"""Convert object to dict so that it can be serialized with msgpack"""
d = {k: getattr(self, k) for k in self.__annotations__}
d["op"] = self.op
return d


# Note: as of Python 3.10.2, @dataclass(slots=True) doesn't work with __init__subclass__
# https://bugs.python.org/issue46970
@dataclass
class TaskFinishedMsg(SendMessageToScheduler, op="task-finished"):
class TaskFinishedMsg(SendMessageToScheduler):
op = "task-finished"

key: str
nbytes: int | None
type: bytes # serialized class
Expand All @@ -298,7 +295,9 @@ def to_dict(self) -> dict[str, Any]:


@dataclass
class TaskErredMsg(SendMessageToScheduler, op="task-erred"):
class TaskErredMsg(SendMessageToScheduler):
op = "task-erred"

key: str
exception: Exception
exception_text: str
Expand All @@ -315,28 +314,36 @@ def to_dict(self) -> dict[str, Any]:


@dataclass
class ReleaseWorkerDataMsg(SendMessageToScheduler, op="release-worker-data"):
class ReleaseWorkerDataMsg(SendMessageToScheduler):
op = "release-worker-data"

__slots__ = ("key",)
key: str


@dataclass
class RescheduleMsg(SendMessageToScheduler, op="reschedule"):
class RescheduleMsg(SendMessageToScheduler):
op = "reschedule"

# Not to be confused with the distributed.Reschedule Exception
__slots__ = ("key", "worker")
key: str
worker: str


@dataclass
class LongRunningMsg(SendMessageToScheduler, op="long-running"):
class LongRunningMsg(SendMessageToScheduler):
op = "long-running"

__slots__ = ("key", "compute_duration")
key: str
compute_duration: float


@dataclass
class AddKeysMsg(SendMessageToScheduler, op="add-keys"):
class AddKeysMsg(SendMessageToScheduler):
op = "add-keys"

__slots__ = ("keys", "stimulus_id")
keys: list[str]
stimulus_id: str

0 comments on commit 297bed6

Please sign in to comment.