You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python multiprocessing无法充分利用多核问题求助

The Root Cause & Fix for Your Multiprocessing CPU Utilization Issue

I see exactly what's going on here—your code isn't actually spawning separate processes to run in parallel, which is why you're only using a fraction of your CPU.

The critical mistake is in how you're initializing your multiprocessing.Process objects. When you write p1 = multiprocessing.Process(target=f(input1,1)), you're immediately executing the f function in the main process before creating the Process object. That means your code runs f(input1,1) synchronously, waits for it to finish, then moves on to create the next process (which does the same thing). No parallelism at all—just sequential execution on one core, hence the ~20% CPU usage on your 4-core system.

Here's the Corrected Code

import multiprocessing
import time

def f(x, y):
    local_start = time.time()
    for _ in range(x):
        x *= 2
    print(y, time.time() - local_start)

if __name__ == "__main__":
    input1 = 500000
    # Initialize processes correctly: pass the function, not its execution result
    p1 = multiprocessing.Process(target=f, args=(input1, 1))
    p2 = multiprocessing.Process(target=f, args=(input1, 2))
    
    # Start the processes—this launches them in parallel
    p1.start()
    p2.start()
    
    # Optional: Uncomment if you want the main process to wait for both to finish
    # p1.join()
    # p2.join()

Key Fixes Explained

  1. Correct Target Assignment: Use target=f instead of target=f(input1,1). The target parameter expects a function object, not the result of calling that function.
  2. Pass Arguments with args: Use the args tuple to pass values to your function. This ensures the function runs with those parameters in the child process, not the main one.
  3. Call .start(): You must call .start() on each Process object to actually spawn the child process and begin execution. Without this, the process won't run at all.

Once you make these changes, your processes will run in parallel, utilizing multiple cores. You should see CPU usage jump to 50% or higher (depending on how many processes you spawn) as your 4-core system handles tasks simultaneously.

内容的提问来源于stack exchange,提问作者Sarper Bilazer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:37:38