Created
November 20, 2022 23:22
-
-
Save wkettler/d4bb48af98778467c7eadf0e59fc885e to your computer and use it in GitHub Desktop.
Python3 multiprocessing loggin example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import logging | |
import multiprocessing | |
def create_logger(): | |
logger = multiprocessing.get_logger() | |
logger.setLevel(logging.INFO) | |
formatter = logging.Formatter(\ | |
'[%(asctime)s| %(levelname)s| %(processName)s] %(message)s') | |
handler = logging.FileHandler(f'{__file__}.log') | |
handler.setFormatter(formatter) | |
# this bit will make sure you won't have | |
# duplicated messages in the output | |
if not len(logger.handlers): | |
logger.addHandler(handler) | |
return logger | |
def worker(id): | |
logger = create_logger() | |
logger.info(f'process {id} checking in') | |
if __name__ == '__main__': | |
count = 5 | |
logger = create_logger() | |
logger.info('starting pool') | |
with multiprocessing.Pool(count) as pool: | |
pool.map(worker, [x for x in range(10)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment