Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parallel class method calls in Python
# Reference: https://stackoverflow.com/questions/50528331/parallel-class-function-calls-using-python-joblib
# Require: Joblib (https://joblib.readthedocs.io/en/latest/index.html)
from joblib import Parallel, delayed
# Defining a sample object with a time-consuming calculate() method
class Node(object):
def __init__(self, x):
self.x = x
self.result = None
def calculate(self):
self.result = self.x**2
def computing_node(node):
node.calculate()
return node
# Generating list of nodes, which will contain all tasks to be run
nodes = [Node(x) for x in range(100)]
# Parallel class method calling using Joblib
with Parallel(n_jobs=5, verbose=10) as parallel:
delayed_funcs = [delayed(computing_node)(node) for node in nodes]
results = parallel(delayed_funcs)
nodes = results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment