Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Parallel class method calls in Python
# Reference:
# Require: Joblib (
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):
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