logging in multiprocess writes to same log

27 Views Asked by At

I wrote a python code the runs a function as a process in a multiprocess way.

For simplicity, I paste a simplified code that looks like my code in general:

from multiprocessing import Pool
import time
import logging


def my_function(number):

    logging.basicConfig(filename=f"{number}.log", levl=logging.INFO)
    square = number * number
    time.sleep(1)
    logging.info(f"Square of {number} is {square}")


if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]

    pool = Pool()
    for number in numbers:
        pool.apply_async(my_function, number)

    pool.close()
    pool.join()

    print("All processes started.")

Basically what I try to do is to create processes in a for loop where each process has its own log file.

for some reason, in my code, the log files are created, but the written data is sometimes mixed between files.

for example, there is a chance that I will have in the 5.log file the message "Square of 5 is 25" and "Square of 1 is 1".

Why won't each message be written in its specific file?

Thank you

0

There are 0 best solutions below