Hi,

I'm trying to get Logbook working for multiple processes without nailing 
the CPU. Below is a simplified example of my usage.

Does anyone have any ideas, or sample code they can share? The examples in 
the Logbook test suite are too simple.

Cheers, Roger


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing as mp
import sys
import time

import logbook
from logbook.queues import MultiProcessingHandler, MultiProcessingSubscriber


class input_worker(object):

    def __init__(self, stopevent, logqueue, loghandler):
        self.stopevent = stopevent
        self.handler = MultiProcessingHandler(logqueue)

    def __call__(self):
        while (not self.stopevent.is_set()):
            time.sleep(1.0)
            with self.handler: #.threadbound(): # do I need this?
                logbook.notice("%s: %d" % (mp.current_process().name, 
time.time()))

stopevent = mp.Event()
logqueue = mp.Queue(-1)
loghandler = logbook.NestedSetup([
    logbook.NullHandler(),
    # logbook.FileHandler(test_logbook.log, mode='w', level='DEBUG'),
    logbook.StreamHandler(sys.stdout, level='NOTICE', bubble=True)
])
subscriber = MultiProcessingSubscriber(logqueue)

processes = [ mp.Process(target=input_worker(stopevent, logqueue, loghandler)) 
for _ in range(3) ]

controller = subscriber.dispatch_in_background(loghandler)

with loghandler:
    logbook.notice("Process started!")

for p in processes:
    p.start()

# Process 10 seconds of data
time.sleep(10.0)

stopevent.set()

for p in processes:
    p.join()

controller.stop()

with loghandler:
    logbook.notice("Process finished!")


-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pocoo-libs.
For more options, visit https://groups.google.com/d/optout.

Reply via email to