Python多处理不使用可用核心 [英] Python multiprocessing NOT using available Cores

查看:81
本文介绍了Python多处理不使用可用核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了一个简单的Python程序-分别进行4个处理.我希望程序在4秒钟内完成执行(如您在代码中所见),但是它需要10秒钟-这意味着它不会执行并行处理.我的CPU中有1个以上的内核,但是该程序似乎只使用了1个.请指导我在这里如何实现并行处理?谢谢.

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")

推荐答案

每当您在某个进程上调用join时,您的执行块就会等待该进程完成.因此,在您的代码中,您总是在等待下一个过程完成之前才开始下一个过程.所有进程启动后,您需要保留对进程的引用,并对其进行join引用.像这样:

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")

这篇关于Python多处理不使用可用核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆