使用并发futures ProcessPoolExecutor时登录主进程



关于如何让分叉进程登录到主进程,有什么想法吗?

我的设置是这样的:


from itertools import repeat
from concurrent.futures import ProcessPoolExecutor
logger = logging.getLogger(__name__)
def worker_process(i, logger):
logger.info(f"{i}")
with ProcessPoolExecutor(max_workers=4) as executor:
logger.info("Starting")
executor.map(worker_process, range(5), repeat(logger))

您的工作进程确实会登录到主线程。您没有看到任何日志输出,因为logger的默认日志级别高于INFO

将日志记录设置为INFO,您将看到输出:

from itertools import repeat
from concurrent.futures import ProcessPoolExecutor
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def worker_process(i, logger):
logger.info(f"{i}")

with ProcessPoolExecutor(max_workers=4) as executor:
executor.map(worker_process, range(5), repeat(logger))

结果:

INFO:__main__:0
INFO:__main__:4
INFO:__main__:3
INFO:__main__:1
INFO:__main__:2

相关内容

  • 没有找到相关文章

最新更新