Thread KeyError catch
Hy in my python thread script had sometimes occured KeyError which I could not catch. The error looks like:
Unhandled exception in thread started by <bound method Thread._bootstrap of <timerRewriteChecker(Thread-570, started 1971645664)>> Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 882, in _bootstrap self._bootstrap_inner() File "/usr/lib/python3.5/threading.py", line 906, in _bootstrap_inner del _limbo[self] KeyError: <timerRewriteChecker(Thread-570, started 1971645664)>
My code is:
class timerRewriteChecker(StoppableThread): def __init__(self): self.thread_flag = False pass def put(self, device): self._time = time.time() self._device = device try: if not self.thread_flag or self.stopped or not self.is_alive(): StoppableThread.__init__(self) self.start() except Exception as e: print(e) def run(self): try: self.thread_flag = True last_rewrite = time.time() while not self.stopped: time_buff = self._time time.sleep(0.002) if self._time == time_buff or time.time() - last_rewrite > 0.2: self._device._RewriteDisplay() if self._time == time_buff: self.stop() else: last_rewrite = time.time() self.thread_flag = False except KeyError as e: self.put(self._device) _rewriteDisplay = timerRewriteChecker()
The thread is starting while put function has called and of cocurse thread was not started.
None of exceptions in code works. The run script calls self._device._RewriteDisplay() whis has exception catching too.
Any idea how can I solve this? Thanks Mike