简体   繁体   English

为什么我的 python 多处理不能使用所有 cpu?

[英]why my python multiprocessing can't use all cpu?

i have a server with 48 cores, so i decide to use python multiprocess to run my code with 46 process.我有一个有 48 个内核的服务器,所以我决定使用 python 多进程来运行我的 46 个进程的代码。

here is the multiprocessing wrapper function:这是多处理包装器 function:

import multiprocessing
def MPRun(func, args, dicts={}, n_pool=max(1, multiprocessing.cpu_count()-2), ret=True, show_process=True, show_num=100):
  print('submit a multiprocess task with cpu', n_pool)
  q = Queue()
  if not isinstance(args, list) or not isinstance(dicts, dict):
    print('args should be list, dicts should be dict', type(args), type(dicts))
    sys.exit(1)
  pool = multiprocessing.Pool(processes=n_pool)
  res = []
  for i, arg in enumerate(args):
    if not isinstance(args[0], list):
      q.put(pool.apply_async(func, tuple([arg]), dicts))
    else:
      q.put(pool.apply_async(func, tuple(arg), dicts))
  count = 0 
  r = []
  while count < len(args):
    r.append(q.get().get())
    count += 1
    if show_process and count % show_num == show_num - 1:
      print('MPRUN process[%d/%d]'%(count, len(args)))
  pool.close()                                                                                                                                                                               
  pool.join()
  return r

I call it like this:我这样称呼它:

MPRun(myfunc, [[task] for task  in tasks])

when i call top command in linux, i found it do has 46 python process, but among them, only 8-12 is running, the left's state are s(which means wait cpu to run).当我在 linux 中调用 top 命令时,我发现它确实有 46 个 python 进程,但其中只有 8-12 个正在运行,左边的 state 是 s(等待 cpu 运行)。 and many cpu's idle is 100%(which means it is idle)许多cpu的空闲是100%(这意味着它是空闲的)

there is no other code running, so, i am confused, why those idle cpu dont run the state s tasks?没有其他代码在运行,所以,我很困惑,为什么那些空闲的 cpu 不运行 state 的任务?

Is there any problems with my MPRun()?我的 MPRun() 有什么问题吗? or is it related with my task type?还是与我的任务类型有关?

You didn't show what your tasks actually do.您没有显示您的任务实际做什么。 There could be many reasons why they are not using full CPU.他们没有使用完整 CPU 的原因可能有很多。 's' - means that the task is waiting on something to happen it could be one of the following: 's' - 表示任务正在等待某事发生,它可能是以下之一:

  • Waiting for response to the.network request等待对 .network 请求的响应
  • Waiting on synchronization object等待同步 object
  • Waiting for swapped out page to load from memory ( this is very common on very busy systems, did you allocate more memory than your system can handle?)等待换出的页面从 memory 加载(这在非常繁忙的系统上很常见,您分配的 memory 是否超出了系统的处理能力?)
  • Waiting on the IO request ( are your tasks IO bound or CPU bound )等待 IO 请求(您的任务是 IO 绑定还是 CPU 绑定)
  • Other things that a program can wait for程序可以等待的其他事情

Try to profile your processes with 'strace' and see if there something going on there.尝试使用“strace”分析您的流程,看看那里是否发生了什么。

Also look at your /proc/interrupts to see if you have unsually high IO activity.还要查看您的 /proc/interrupts 以查看您是否有异常高的 IO 活动。 You have to take a baseline first to determine the interrupts volume on your system when it's idle, and then sample it again while your program is running您必须首先获取基线以确定系统空闲时的中断量,然后在程序运行时再次对其进行采样

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

相关问题 为什么多处理不使用我所有的内核 - why doesn't multiprocessing use all my cores 为什么我的 Python 程序平均每个进程只占用 33% 的 CPU? 如何让 Python 使用所有可用的 CPU? - Why does my Python program average only 33% CPU per process? How can I make Python use all available CPU? 为什么我的 python 代码没有使用所有的 cpu? - why isn't my python code using all of the cpu? 为什么多处理在Google Compute Engine中不使用100%CPU? - Why doesn't Multiprocessing use 100% CPU in Google Compute Engine? Python multiprocessing.Pool()不使用每个CPU的100% - Python multiprocessing.Pool() doesn't use 100% of each CPU 为什么我的 CPU 核心数是 2,但使用 multiprocessing.cpu_count() 得到 4? - Why my CPU core number is 2, but use multiprocessing.cpu_count() get 4? 不能在python函数中使用多处理 - Can't use multiprocessing in python function PYTHON-为什么多处理池使用100%的CPU? - PYTHON - why multiprocessing Pool using 100% of CPU? 为什么我不能在 python 多处理中关闭池之前使用 join() - Why can't I use join() before closing pool in python multiprocessing Python多处理并未使用RHEL6上的所有内核 - Python multiprocessing doesn't use all cores on RHEL6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM