From 3ec0903903aec12b01c3eef42c8eb944fe00c90f Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Thu, 8 Jun 2023 18:38:07 -0700 Subject: [PATCH] Simple base interfaces. --- src/pyutils/collectionz/shared_dict.py | 4 +++- src/pyutils/parallelize/thread_utils.py | 4 +++- src/pyutils/typez/typing.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/pyutils/collectionz/shared_dict.py b/src/pyutils/collectionz/shared_dict.py index dba30e5..e3324cb 100644 --- a/src/pyutils/collectionz/shared_dict.py +++ b/src/pyutils/collectionz/shared_dict.py @@ -48,6 +48,8 @@ from typing import ( ValuesView, ) +from pyutils.typez.typing import Closable + class PickleSerializer: """A serializer that uses pickling. Used to read/write bytes in the shared @@ -71,7 +73,7 @@ class PickleSerializer: # whole thing? -class SharedDict(object): +class SharedDict(Closable): """This class emulates the dict container but uses a `Multiprocessing.SharedMemory` region to back the dict such that it can be read and written by multiple independent processes at the diff --git a/src/pyutils/parallelize/thread_utils.py b/src/pyutils/parallelize/thread_utils.py index 43e6294..4e891f1 100644 --- a/src/pyutils/parallelize/thread_utils.py +++ b/src/pyutils/parallelize/thread_utils.py @@ -10,6 +10,8 @@ import os import threading from typing import Any, Callable, Optional, Tuple +from pyutils.typez.typing import Runnable + # This module is commonly used by others in here and should avoid # taking any unnecessary dependencies back on them. @@ -132,7 +134,7 @@ def background_thread( return wrapper(_funct) -class ThreadWithReturnValue(threading.Thread): +class ThreadWithReturnValue(threading.Thread, Runnable): """A thread whose return value is plumbed back out as the return value of :meth:`join`. Use like a normal thread:: diff --git a/src/pyutils/typez/typing.py b/src/pyutils/typez/typing.py index 18cf4ef..bed5e7f 100644 --- a/src/pyutils/typez/typing.py +++ b/src/pyutils/typez/typing.py @@ -28,3 +28,24 @@ class Comparable(Protocol): @abstractmethod def __eq__(self, other: Any) -> bool: ... + + +class Closable(Protocol): + """Something that can be closed.""" + + def close(self) -> None: + ... + + +class Cloneable(Protocol): + """Something that can be cloned.""" + + def clone(self) -> None: + ... + + +class Runnable(Protocol): + """Something that can be run.""" + + def run(self) -> None: + ... -- 2.46.0