问题
I ran below simple Python program - to do 4 processes separately. I expect the program completes execution in 4 seconds (as you can see in the code), but it takes 10 seconds - meaning it does not do parallel processing. I have more than 1 core in my CPU, but the program seems using just one. Please guide me how can I achieve parallel processing here? Thanks.
import multiprocessing
import time
from datetime import datetime
def foo(i):
print(datetime.now())
time.sleep(i)
print(datetime.now())
print("=========")
if __name__ == '__main__':
for i in range(4,0,-1):
p = multiprocessing.Process(target=foo, args=(i,))
p.start()
p.join()
print("Done main")
回答1:
Whenever you call join
on a process, your execution block and waits for that process to finish. So in your code, you always wait before for the last process to finish before starting the next one. You need to keep a reference to the processes and join
to them after all of them have started, eg. like this:
if __name__ == '__main__':
processes = [multiprocessing.Process(target=foo, args=(i,))
for i in range(4,0,-1)]
for p in processes:
p.start()
for p in processes:
p.join()
print("Done main")
来源:https://stackoverflow.com/questions/61295683/python-multiprocessing-not-using-available-cores