From 8ca3649dcff72ef580d1377aa497a9269e47ba4f Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 11 Jul 2022 11:31:14 +1000 Subject: [PATCH] python-stdlib/threading: Add Thread.join function. --- python-stdlib/threading/threading.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/python-stdlib/threading/threading.py b/python-stdlib/threading/threading.py index d72483df..943dcec5 100644 --- a/python-stdlib/threading/threading.py +++ b/python-stdlib/threading/threading.py @@ -15,12 +15,34 @@ class Thread: self.args = args self.daemon = None self.kwargs = {} if kwargs is None else kwargs + self.ident = None + + self._started = False + self._lock = Lock() + self._ret = None + self._ex = None def start(self): + self._lock.acquire() _thread.start_new_thread(self.run, ()) + self._started = True def run(self): - self.target(*self.args, **self.kwargs) + self.ident = _thread.get_ident() + try: + self._ret = self.target(*self.args, **self.kwargs) + except Exception as ex: + self._ex = ex + self._lock.release() + + def join(self, timeout=None): + if not self._started: + raise RuntimeError("cannot join thread before it is started") + self._lock.acquire(True, timeout) + if self._ex: + raise self._ex + return self._ret + Lock = _thread.allocate_lock