0

How to use multiprocessing pool to call a method of a class which has parametrized constructor? Below is my example program. Here I need to execute obj.api_call() method in parallel using pool of worker processes.

import requests


class A:
    def __init__(self, user):
        self.user = user

    def api_call(self):
        url = "http://eample.com?name=" + self.user
        print(url)
        res = requests.get(url)
        print(res)


name_list = ['ram', 'krish', 'shiv', 'david', 'rahul', 'gopal', 'vijay', 'sati']
for name in name_list:
    obj = A(name)
    obj.api_call()
owgitt
  • 313
  • 5
  • 22
  • Your question is a bit misleading. *Parameterized constructor* would lead one to assume that you are asking about [generics in Python](https://stackoverflow.com/questions/6725868/generics-templates-in-python) which is not the case. – Janez Kuhar Sep 25 '20 at 09:58

1 Answers1

1

An easy manner to do it would be to create a function that takes care of the instantiation as well as the call:

def mp_func(user):
    a = A(user)
    a.api_call()

and call that function in your pool.

If you want to really make it so that it works with a function from your class maybe you can use a classmethod:

class A(object):
    def __init__(self, user):
        self.user = user

    def api_call(self):
        print(self.user)

    @classmethod
    def mp_create_and_call(cls, user):
        newcls = cls(user)
        newcls.api_call()

import multiprocessing as mp
pool = mp.Pool(4)
usernames = ['a', 'b', 'c'] * 20
pool.map(A.mp_create_and_call, usernames)
Marc
  • 1,539
  • 8
  • 14