I'm a newcomer w.r.t. parallel processing, and I'd like to ask for a bit of help here please.
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.
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
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))
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
works for me