Skip to content

Instantly share code, notes, and snippets.

@migurski
Created January 14, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save migurski/e73faf00d7e1c5e44bd6 to your computer and use it in GitHub Desktop.
Save migurski/e73faf00d7e1c5e44bd6 to your computer and use it in GitHub Desktop.
Alternative logging arrangement for OpenAddresses machine
def _run_process_one(lock, source_queue, destination, source_extras, results):
''' Single queue worker for source files to conform().
Keep going until source_queue is empty.
'''
while True:
with lock:
if not source_queue:
return
path = source_queue.pop(0)
extras = source_extras.get(path, dict())
try:
if not isdir(destination):
try:
mkdir(destination)
except OSError:
pass
_L.info(path)
#
# Filter class to match only records in the current thread.
#
class ThreadFilter:
def __init__(self, id):
self.id = id
def filter(self, record):
return record.thread == self.id
#
# LoggingHandler that writes to a stream.
#
from io import BytesIO
stream = BytesIO()
new_handler = logging.StreamHandler(stream)
new_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)06s: %(message)s'))
new_handler.setLevel(logging.DEBUG)
new_handler.addFilter(ThreadFilter(thread.get_ident()))
logging.getLogger('openaddr').addHandler(new_handler)
#
# Do the work
#
result = process_one.process(path, destination, extras)
#
# Remove the handler and peek into the log contents.
#
logging.getLogger('openaddr').removeHandler(new_handler)
_L.info(repr(stream.getvalue()))
except:
_L.error('Error while running process_one.process', exc_info=True)
result = None
with lock:
results[path] = result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment