Melissa Stewart
Melissa Stewart

Reputation: 3635

Instantiate an object an pass it into a multiprocessing pool in Python

I've an object which has a method which I want to multiprocess.

class Driver:
   def __init__(arg1, arg2, arg3):
      #initialize
   def do_work(self, key):
      #do some work

I have a collection of items, I want to instantiate a driver object and pass the do_work function to work on 1 item of a collection.

arr = [# collection of strings]

with multiprocessing.Pool(processes=8) as pool:
     _ = pool.map(# I want to instantiate the driver and call the do_work on a one item of an array, arr)

I'm new to multiprocessing.

Upvotes: 0

Views: 267

Answers (1)

Pramit Mazumder
Pramit Mazumder

Reputation: 80

Just instantiate the class as normal. Since this is only one job, you could use apply rather than map, and this allows you to use multiple arguments as well:

with multiprocessing.Pool(processes=8) as pool:
     driver = Driver()
     _ = pool.apply(driver.do_work, (driver, arr[some element]))

Upvotes: 2

Related Questions