0

this is driving me insane. I'm working with python3 multiprocessing pool of size 3, and I'm creating a log for each process. Only the first 3 logs are created!!! what am I missing?

from time import sleep
import logging, os, multiprocessing as mp

def do_it(proc_num):
    log_file = f'{proc_num}.log'
    print(f"start {proc_num} {os.getpid()} {log_file}")
    logging.basicConfig(filename=log_file, level=logging.INFO)
    try:
        for iter in range(5):
            sleep(1)
            logging.info(f"{proc_num}: hello {iter}")
    finally:
        print(f"finalizing {proc_num}")
        logging.shutdown()
        logger = logging.getLogger()
        logger.handlers[0].flush()
        sleep(3)

if __name__ == '__main__':
    pool = mp.Pool(3)
    pool.map(do_it, [1, 2, 3, 4, 5, 6, 7])
    pool.close()

and the result:

ls *.log
1.log 2.log 3.log

wat?!?!


NOTE: NOT the same as How should I log while using multiprocessing in Python? because I WANT a separate logfile per process.

ihadanny
  • 4,377
  • 7
  • 45
  • 76
  • What do your print statements show? And did you read the [docs](https://docs.python.org/3/library/logging.html#logging.basicConfig) for basicConfig, where it says, "This function does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True." – FiddleStix May 05 '20 at 10:16

1 Answers1

0

ok, I should have just look at the logs to see the answer...

cat 3.log 
INFO:root:3: hello 0
INFO:root:3: hello 1
INFO:root:3: hello 2
INFO:root:3: hello 3
INFO:root:3: hello 4
INFO:root:4: hello 0
INFO:root:4: hello 1
INFO:root:4: hello 2
INFO:root:4: hello 3
INFO:root:4: hello 4

apparently the same log file is used for all tasks done by this process

ihadanny
  • 4,377
  • 7
  • 45
  • 76