Update requirements to include psutil.
[python_utils.git] / thread_utils.py
index c4a293794a99cb1f479da105a499cb2ce93b564e..df637e043e94abb78ea2fc6421213e753c3ba70d 100644 (file)
@@ -120,6 +120,27 @@ def background_thread(
         return wrapper(_funct)
 
 
         return wrapper(_funct)
 
 
+class ThreadWithReturnValue(threading.Thread):
+    """A thread whose return value is plumbed back out as the return
+    value of :meth:`join`.
+    """
+
+    def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None):
+        threading.Thread.__init__(
+            self, group=None, target=target, name=None, args=args, kwargs=kwargs
+        )
+        self._target = target
+        self._return = None
+
+    def run(self):
+        if self._target is not None:
+            self._return = self._target(*self._args, **self._kwargs)
+
+    def join(self, *args):
+        threading.Thread.join(self, *args)
+        return self._return
+
+
 def periodically_invoke(
     period_sec: float,
     stop_after: Optional[int],
 def periodically_invoke(
     period_sec: float,
     stop_after: Optional[int],
@@ -133,11 +154,11 @@ def periodically_invoke(
             call forever
 
     Returns:
             call forever
 
     Returns:
-        a :class:Thread object and an :class:Event that, when
+        a :class:`Thread` object and an :class:`Event` that, when
         signaled, will stop the invocations.
 
     .. note::
         signaled, will stop the invocations.
 
     .. note::
-        It is possible to be invoked one time after the :class:Event
+        It is possible to be invoked one time after the :class:`Event`
         is set.  This event can be used to stop infinite
         invocation style or finite invocation style decorations.
 
         is set.  This event can be used to stop infinite
         invocation style or finite invocation style decorations.