简体   繁体   English

Python:如何使用多重处理来加快对类实例的操作?

[英]Python: How can i use multiprocessing to speed up operating on class instances?

for example: 例如:

instances = [ClassName() for i in range(no_inst)]
for data in dataset:
    for inst in instances:
        inst.dosomething(data)

Note: The instances do not modify the data, so there is no need of communication between the instances. 注意:实例不会修改数据,因此实例之间无需进行通信。

The easiest way that I know of is to use multiprocessing.Pool.map the same way you would use the map builtin. 我所知道的最简单的方法是使用multiprocessing.Pool.map ,就像使用内置的map You just need to write a function to do the task that you want to complete and it handles all of the parallelism: 您只需要编写一个函数来完成您想要完成的任务,它就可以处理所有并行性:

import multiprocessing as mp

instances = [ClassName() for i in range(no_inst)]

def dosomething(inst):
    for data in dataset:
        inst.dosomething(data)

num_procs = 4 #however many processors you'll use
pool = mp.Pool(num_procs)
pool.map(dosomething,instances)

there are a few gotchas -- lambda functions won't work here since they aren't pickleable for instance. 有几个陷阱- lambda ,因为它们不是与pickle例如功能将不会在这里工作。 This also returns a list of None (since that is what dosomething returns) -- normally I don't like using list-comprehensions or map for side-effects, but I relax my stance on that when it comes to multiprocessing simply because it is so easy . 这也将返回列表None (因为这是dosomething回报) -通常我不喜欢使用列表推导或map的副作用,但是我放松对我的立场,当它涉及到多处理器只是因为它是这么容易

You can use the threading interface to create a Thread class that fulfills the dosomething(data) . 您可以使用threading接口创建一个满足dosomething(data)Thread类。 You can start multiple thread for each loop, calling run() to start them. 您可以为每个循环启动多个线程,调用run()启动它们。 It's recommended to then put them into a list so that you can check their status until all have completed. 建议将它们放入列表,以便您可以检查它们的状态,直到全部完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM