Joblib与对象 [英] Joblib with objects
问题描述
我是新来的.并行处理,请在这里寻求帮助.
I'm a newcomer w.r.t. parallel processing, and I'd like to ask for a bit of help here please.
我有一个对象需要在内存中复制,大约50到100次甚至更多.这是在生物病毒模拟器中使用的,在那里我尝试模拟病毒在细胞内的复制和爆发.
I have an object that I need to replicate in memory, something like 50-100 times or even more. This is used in a biological virus simulator, where I try to simulate the replication and bursting of a virus inside a cell.
我现在正在使用的代码使用Deepcopy(请不要判断),并且具有以下逻辑:
The code I am using right now uses deepcopy (please don't judge), and has the following logic:
from copy import deepcopy
from random import random
from datetime import datetime
import hashlib
class Virus(object):
def __init__(self):
self.id = None
self.SetID()
def SetID(self):
random_number = str(random())
current_time = str(datetime.now())
unique_string = random_number + current_time
unique_id = hashlib.new('sha512')
unique_id.update(unique_string)
self.id = unique_id.hexdigest()
def GetID(self):
return self.id
def Replicate(self):
new_virus = deepcopy(self)
new_virus.SetID()
return new_virus
由于我的目标是生成病毒的多个副本,但是我可能需要多次迭代,因此我想到了使用joblib
来加快速度. (我可能是错的,如果是这样,请纠正我!)因此,我尝试了以下代码:
Since my goal is to generate multiple copies of the virus, but I may potentially need to iterate many times, I thought of using joblib
to speed things up. (I may be wrong, please correct me if this is so!) Therefore, I tried the following code:
h = Virus()
results = Parallel(n_jobs=2)(delayed(h.Replicate())(h) for i in range(10))
我希望结果是10种病毒的列表,但我却收到TypeError说:
I expected results to be a list of 10 viruses, but instead, I get a TypeError saying:
TypeError: 'Virus' object is not callable
我似乎无法回避正在发生的事情.我该如何启用它?
I can't seem to wrap my head around what is going on. How can I enable this to work?
推荐答案
延迟用于包装函数(不是对象,现在将返回复制的病毒).所以不要在延迟通话中拨打复制"
delayed is to wrap a function (not an object, which would now return a replicated Virus). so do not call Replicate in delayed call
但是似乎您也无法包装实例方法,因此请使复制成为一个常规函数,该函数将复制其参数
but also it seems you would not be able to wrap an instance method, so make replicate a regular function which would replicate its argument
def replicate(virus):
return virus.Replicate()
h = Virus()
results = Parallel(n_jobs=2)(delayed(replicate)(h) for i in range(10))
print [x.id for x in results]
为我工作
这篇关于Joblib与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!