I made an simple example to describe the problem I am facing.
code example1:
from multiprocessing import Pool
from functools import partial
metric # a class instance which includes several pandas dataframe as class variable. This instance is defined globally
variables # a list of size 2000k
def get_reward(variable_, metric_, *args):
...
reward = metric_.some_method(variable_)
return reward
func_ = partial(get_reward, metric_=metric)
mp_workers = Pool(64)
map_ = mp_workers.map(func_, variables)
code example2:
from multiprocessing import Pool
from functools import partial
metric # a class instance which includes several pandas dataframe as class variable. This instance is defined globally
variables # a list of size 2000k
def get_reward(variable_, *args):
...
reward = metric.some_method(variable_)
return reward
mp_workers = Pool(64)
map_ = mp_workers.map(get_reward, variables)
In a nutshell, the difference between the two code examples is that: the 'get_reward' function in the second code doesn't take the class instance (metric) as argument and just use it as the global instance, while the 'get_reward' function in the first code take the class instance as an argument.
I found that the first code work very slowly while the second code finely works. The reason may be that the first code copies the big class instance to every worker and it takes quite a long time.
In my real code, I can only have the structure like the first code example, in which the 'get_reward' function should take the class instance as an argument.
Any way to accelerate the code example 1?
Thanks.