Skip to content

Commit

Permalink
Fixed up code issues highlighed by deepsource
Browse files Browse the repository at this point in the history
  • Loading branch information
rwb27 committed Mar 27, 2020
1 parent a3903eb commit 97c3e53
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions labthings/core/tasks/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, target=None, name=None, args=None, kwargs=None, daemon=True):
# Public state properties
self.progress: int = None # Percent progress of the task
self.data = {} # Dictionary of custom data added during the task
self.log = [] # The log will hold dictionary objects with log information
self.log = [] # The log will hold dictionary objects with log information

# Stuff for handling termination
self._running_lock = Lock() # Lock obtained while self._target is running
Expand Down Expand Up @@ -112,8 +112,8 @@ def wrapped(*args, **kwargs):
self._status = "error"
finally:
self._end_time = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
logging.getLogger().removeHandler(handler) # Stop logging this thread specially

logging.getLogger().removeHandler(handler) # Stop logging this thread
# If we don't remove the handler, it's a memory leak.
return wrapped

def run(self):
Expand Down Expand Up @@ -209,11 +209,27 @@ def terminate(self):
self._status = "terminated"
self.progress = None


class ThreadLogHandler(logging.Handler):
def __init__(self, thread=None, dest=[]):
def __init__(self, thread=None, dest=None):
"""Set up a log handler that appends messages to a list.
This log handler will first filter by ``thread``, if one is
supplied. This should be a ``threading.Thread`` object.
Only log entries from the specified thread will be
saved.
``dest`` should specify a list, to which we will append
each log entry as it comes in. If none is specified, a
new list will be created.
NB this log handler does not currently rotate or truncate
the list - so if you use it on a thread that produces a
lot of log messages, you may run into memory problems.
"""
logging.Handler.__init__(self)
self.thread = thread
self.dest = dest
self.dest = dest if dest else []
self.addFilter(self.check_thread)

def check_thread(self, record):
Expand All @@ -222,8 +238,7 @@ def check_thread(self, record):
return 1
if record.thread == self.thread.ident:
return 1
else:
return 0
return 0

def emit(self, record):
"""Do something with a logged message"""
Expand All @@ -233,4 +248,5 @@ def emit(self, record):
"level": record.levelname,
"message": record.getMessage(),
})

# FIXME: make sure this doesn't become a memory disaster!
# We probably need to check the size of the list...

0 comments on commit 97c3e53

Please sign in to comment.