Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
ErrThreadMixin
to bubble up exceptions from ReaderThread
- Loading branch information
Showing
2 changed files
with
47 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import threading | ||
import logging | ||
from typing import Optional | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class StoppableThreadMixin: | ||
def __init__(self, *args, **kwargs): | ||
self._stop_event = threading.Event() | ||
super().__init__(*args, **kwargs) | ||
|
||
def stop(self): | ||
self._stop_event.set() | ||
|
||
def is_stopped(self): | ||
return self._stop_event.is_set() | ||
|
||
|
||
class ErrThreadMixin(StoppableThreadMixin): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._error: Optional[Exception] = None | ||
|
||
def get_error(self): | ||
return self._error | ||
|
||
def error(self, exc): | ||
logger.error("got exception %r, shutting down thread", exc) | ||
self._error = exc | ||
self.stop() | ||
|
||
def maybe_raise(self): | ||
if self._error is not None: | ||
raise self._error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters