(continued from previous page) (continues on next page) 15 name =multiprocessing.current_process() .name print('Worker started: %s '%name) for i in range(10 ): time.sleep(random()) logger=logging.getLogger(choice(LOGGERS)) level=choice(LEVELS) message =choice(MESSAGES) logger.log(level, message) print('Worker finished: %s '%name) # Here's where the demo gets orchestrated. Create the queue, create and start # the listener, create ten workers and start them, wait for them to finish, # then send a None to the queue to tell the listener to finish. def main(): queue =multiprocessing.Queue(-1) listener =multiprocessing.Process(target =listener_process, args=(queue, listener_configurer)) listener.start() workers =[] for i in range(10 ): worker =multiprocessing.Process(target =worker_process, args=(queue, worker_configurer)) workers.append(worker) worker.start() for w in workers: w.join() queue.put_nowait( None ) listener.join() if __name__ =='__main__ ': main() import logging import logging.config import logging.handlers from multiprocessingimport Process, Queue import random import threading import time def logger_thread(q): while True : record =q.get() if record is None : break logger=logging.getLogger(record.name) logger.handle(record) def worker_process(q): qh =logging.handlers.QueueHandler(q) root =logging.getLogger() root.setLevel(logging .DEBUG) root.addHandler(qh) levels=[logging.DEBUG, logging .INFO, logging.WARNING, logging .ERROR, logging.CRITICAL] loggers=['foo','foo.bar','foo.bar.baz ', 'spam','spam.ham ','spam.ham.eggs '] for i in range(100 ): A variant of the above script keeps the logging in the main process, in a separate thread: