Python multiprocessing无法充分利用多核问题求助
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
- Correct Target Assignment: Use
target=finstead oftarget=f(input1,1). Thetargetparameter expects a function object, not the result of calling that function. - Pass Arguments with
args: Use theargstuple to pass values to your function. This ensures the function runs with those parameters in the child process, not the main one. - 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

