简体   繁体   English

Python池流程管理

[英]Python pool process management

I was running some Python code and realized when I instantiate Pool with only one process, for instance: 我正在运行一些Python代码,并且仅用一个进程实例化Pool时就意识到了这一点,例如:

from multiprocessing.pool import Pool
from time import sleep

def f(i):
  print(i)
  sleep(10)

with Pool(1) as p:
    p.map(f, [i for i in range(100)])

Actually five processes are currently running. 实际上当前正在运行五个进程。 I've also noticed a pattern going on: if I instantiate Pool with 1, 2,3,... processes the number of processes launched by Python are 5,6,7,... I'm curious: Does Pool use three process for management? 我还注意到一种模式:如果我用1,2,3,...实例化Pool ,Python启动的进程数是5,6,7,...我很好奇: Pool是否使用三个过程进行管理?

with Pool(1) , you will get 2 processes, the main process (pid 31070), and one worker process (pid 31071), but 3 extra threads/LWPs in the main process (LWP/thread id 31072/31073/31074): 使用Pool(1) ,您将获得2个进程,主进程(pid 31070)和一个工作进程(pid 31071),但是主进程中有3个额外的线程/ LWP (LWP / thread id 31072/31073/31074) :

  PID  PPID   LWP  NLWP CMD
31070 21240 31070     4 python3 so_48968836_mp.py
31070 21240 31072     4 python3 so_48968836_mp.py
31070 21240 31073     4 python3 so_48968836_mp.py
31070 21240 31074     4 python3 so_48968836_mp.py
31071 31070 31071     1 python3 so_48968836_mp.py

those three threads are for pool workers maintaining, async task and result handling. 这三个线程用于池工作人员维护,异步任务和结果处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM