Python multiprocessing NOT using available Cores

痴心易碎 提交于 2020-05-30 07:55:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!