日志大小为3的多处理池仅为前3个进程创建



这让我发疯了。我正在使用大小为3的python3多处理池,并为每个进程创建一个日志。只创建前3个日志!!!我错过了什么?

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()

结果:

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

瓦特?!?!


注意:与在Python中使用多处理时应该如何登录不同?因为我希望每个进程都有一个单独的日志文件。

好吧,我应该看看日志看看答案。。。

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

显然,相同的日志文件用于该进程完成的所有任务

最新更新