Python 多核编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23537037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python multicore programming
提问by Larry
Please consider a class as follow:
请考虑如下类:
class Foo:
def __init__(self, data):
self.data = data
def do_task(self):
#do something with data
In my application I've a list containing several instances of Foo class. The aim is to execute do_task
for all Foo objects. A first implementation is simply:
在我的应用程序中,我有一个包含 Foo 类的几个实例的列表。目的是do_task
为所有 Foo 对象执行。第一个实现很简单:
#execute tasks of all Foo Object instantiated
for f_obj in my_foo_obj_list:
f_obj.do_task()
I'd like to take advantage of multi-core architecture sharing the for
cycle between 4 CPUs of my machine.
我想利用多核架构for
在我机器的 4 个 CPU 之间共享周期。
What's the best way to do it?
最好的方法是什么?
回答by Dr.Elch
Instead of going through all the multithreading/multicore basics, I would like to reference a Post by Ryan W. Smith: Multi-Core and Distributed Programming in Python
我想参考 Ryan W. Smith 的一篇文章:Python 中的多核和分布式编程,而不是所有的多线程/多核基础知识
He will go into details how you can utilize multiple cores and use those concepts. But please be careful with that stuff if you are not familiar with general multithreading concepts.
他将详细介绍如何利用多个内核并使用这些概念。但是,如果您不熟悉一般的多线程概念,请小心处理这些内容。
Functional Programmingwill also allow you to customize the algorithm/function for each core.
函数式编程还允许您为每个内核自定义算法/函数。
回答by timrau
You can use process poolsin multiprocessing module.
您可以在多处理模块中使用进程池。
def work(foo):
foo.do_task()
from multiprocessing import Pool
pool = Pool()
pool.map(work, my_foo_obj_list)
pool.close()
pool.join()