Skip to content

Commit 640e446

Browse files
author
Joel Collins
committed
Fixed task logging with greenlets
1 parent c981306 commit 640e446

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

labthings/core/tasks/thread.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from gevent import Greenlet, GreenletExit
2+
from gevent.thread import get_ident
23
import datetime
34
import logging
45
import traceback
@@ -54,7 +55,7 @@ def id(self):
5455
@property
5556
def ident(self):
5657
"""Compatibility with threading interface. A small, unique non-negative integer that identifies this object."""
57-
return self.minimal_ident
58+
return get_ident(self)
5859

5960
@property
6061
def state(self):
@@ -124,7 +125,7 @@ def terminate(self):
124125

125126

126127
class ThreadLogHandler(logging.Handler):
127-
def __init__(self, thread=None, dest=None):
128+
def __init__(self, thread=None, dest=None, level=logging.WARNING):
128129
"""Set up a log handler that appends messages to a list.
129130
130131
This log handler will first filter by ``thread``, if one is
@@ -141,17 +142,17 @@ def __init__(self, thread=None, dest=None):
141142
lot of log messages, you may run into memory problems.
142143
"""
143144
logging.Handler.__init__(self)
145+
self.setLevel(level)
144146
self.thread = thread
145-
self.dest = dest if dest else []
147+
self.dest = dest if dest is not None else []
146148
self.addFilter(self.check_thread)
147149

148150
def check_thread(self, record):
149151
"""Determine if a thread matches the desired record"""
150152
if self.thread is None:
151153
return 1
152154

153-
thread_ident = getattr(record.thread, "ident", None)
154-
if record.thread == self.thread.ident:
155+
if get_ident() == get_ident(self.thread):
155156
return 1
156157
return 0
157158

@@ -162,6 +163,7 @@ def emit(self, record):
162163
for k in ["created", "levelname", "levelno", "lineno", "filename"]:
163164
record_dict[k] = getattr(record, k)
164165
self.dest.append(record_dict)
166+
print(self.thread.log)
165167
# FIXME: make sure this doesn't become a memory disaster!
166168
# We probably need to check the size of the list...
167169
# TODO: think about whether any of the keys are security flaws

0 commit comments

Comments
 (0)