Skip to content

Instantly share code, notes, and snippets.

@FanchenBao
Created December 10, 2019 00:33
Show Gist options
  • Save FanchenBao/8708140f0933cf50d861051a62d23b41 to your computer and use it in GitHub Desktop.
Save FanchenBao/8708140f0933cf50d861051a62d23b41 to your computer and use it in GitHub Desktop.
Expanded demo code for Python3 logging with multiprocessing
# logger configuration uncommented in worker_process
def worker_process(queue):
worker_configurer(queue)
for i in range(3):
sleep(random())
innerlogger = logging.getLogger('worker')
innerlogger.info(f'Logging a random number {randint(0, 10)}')
# logger configuration commented out in main
def main():
queue = multiprocessing.Queue(-1)
listener = multiprocessing.Process(
target=listener_process, args=(queue,))
listener.start()
# worker_configurer(queue)
main_logger = logging.getLogger('main')
main_logger.info('Logging from main')
workers = []
for i in range(3):
worker = multiprocessing.Process(target=worker_process, args=(queue,))
workers.append(worker)
worker.start()
for w in workers:
w.join()
main_logger.info('main function ends')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment