使用multiprocessing.Process并发进程数最多

编程入门 行业动态 更新时间:2024-10-26 06:35:46
本文介绍了使用multiprocessing.Process并发进程数最多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有Python代码:

from multiprocessing import Process def f(name): print 'hello', name if __name__ == '__main__': for i in range(0, MAX_PROCESSES): p = Process(target=f, args=(i,)) p.start()

运行良好.但是,MAX_PROCESSES是可变的,并且可以是1和512之间的任何值.由于我仅在具有8内核的计算机上运行此代码,因此我需要确定是否有可能限制允许同时运行的进程数.我已经研究过multiprocessing.Queue,但它看起来不符合我的需要-也许我对文档的解释不正确.

which runs well. However, MAX_PROCESSES is variable and can be any value between 1 and 512. Since I'm only running this code on a machine with 8 cores, I need to find out if it is possible to limit the number of processes allowed to run at the same time. I've looked into multiprocessing.Queue, but it doesn't look like what I need - or perhaps I'm interpreting the docs incorrectly.

是否可以限制同时运行multiprocessing.Process的次数?

Is there a way to limit the number of simultaneous multiprocessing.Processs running?

推荐答案

使用multiprocessing.Pool可能是最明智的选择,它会根据系统上可用的最大内核数生成一个工作进程池,然后进行基本操作核心成为可用的任务.

It might be most sensible to use multiprocessing.Pool which produces a pool of worker processes based on the max number of cores available on your system, and then basically feeds tasks in as the cores become available.

标准文档中的示例( docs. python/2/library/multiprocessing.html#using-a-pool-of-workers )显示,您还可以手动设置内核数:

The example from the standard docs (docs.python/2/library/multiprocessing.html#using-a-pool-of-workers) shows that you can also manually set the number of cores:

from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': pool = Pool(processes=4) # start 4 worker processes result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously print result.get(timeout=1) # prints "100" unless your computer is *very* slow print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"

如果代码中需要的话,知道multiprocessing.cpu_count()方法也可以方便地计算给定系统上的内核数.

And it's also handy to know that there is the multiprocessing.cpu_count() method to count the number of cores on a given system, if needed in your code.

以下一些代码草案似乎适用于您的特定情况:

Here's some draft code that seems to work for your specific case:

import multiprocessing def f(name): print 'hello', name if __name__ == '__main__': pool = multiprocessing.Pool() #use all available cores, otherwise specify the number you want as an argument for i in xrange(0, 512): pool.apply_async(f, args=(i,)) pool.close() pool.join()

更多推荐

使用multiprocessing.Process并发进程数最多

本文发布于:2023-10-28 00:45:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数最多   进程   multiprocessing   Process

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!