Issue
I came across this question:
I wanted to make use of forking and inheritance of memory-objects with python for efficient memory usage in a multiprocessing setting.
(This code example is not to examplify the use of Pool in multiprocessing, it should outline what Python does when starting a new process in a Pool or with calling Process!)
Variant 1
import pickle, time, signal
import multiprocessing as mp
class Compute:
def __init__(self):
self.manager = mp.Manager()
self.manager_dict = self.manager.dict()
self.big_data = pickle.load(open("big_data_object.pkl", "rb"))
def compute(self, task_id):
## do something wigh big data here use self.big_data
## for i in self.big_data:
time.sleep(2)
self.manager_dict[task_id] = "Result"
if __name__ == '__main__':
comp = Compute()
task_id = 1
st = time.time()
p = mp.Process(target=comp.compute, args=(task_id,))
p.start()
p.join()
result = comp.manager_dict[task_id]
print(result)
print("duration: ", time.time() - st)
Variant 2
class Compute:
def __init__(self):
self.big_data = pickle.load(open("big_data_object.pkl", "rb"))
def compute(self, task_id):
## do something wigh big data here use self.big_data
## for i in self.big_data:
time.sleep(2)
return "Result"
if __name__ == '__main__':
comp = Compute()
task_id = 1
compute_input = [task_id]
st = time.time()
with mp.Pool() as p:
for result in p.imap_unordered(comp.compute, compute_input):
print(result)
print("duration: ", time.time()-st)
Then I ran into the following two different methodos to perform this. Unfortunately, the Variant 2 runs twice as long, as the Variant 1.
I could find in debugging that the Pool methodolgy actually does serialize all the objects and deserilizes it again in the new child proccess, which takes of course more time.
The Process variant in 1, does not do this and acutally use inheritance of memory objects.
Could someone with experience in python multiprocessing explain why this is happening here, it does not seem like wanted behaviour for forking in multiprocessing.
(the program needs to be run on Unix/Linux OS for forking)
Solution
This answer is meant to supplement Frank Yellin's answer. I am including this because while a solution was offered, an exact explanation of why this happened was missing from the other post.
As stated in the linked answer, a pool (higher level than a multiprocessing.Process) is meant to survive until you explicitly shut it down. Over it's lifetime it creates and manages a bunch of Processes, supplying them with new work as it comes in. But for this to happen, it needs some way to communicate with the processes it has started. Since multiprocessing.Process does not offer a way to do so directly, pools use a bunch of queues internally to transfer work (this includes arguments to target function, and any return values gathered from workers). So, anything you pass to a pool will be placed on a queue, and this is where the difference occurs.
This is because if you go through the documentation, you will find a note that informs you that whenever an object is put on a queue, it is serialized. Hence start methods are irrelevant, you put something on a queue, it will be pickled. Generalizing, when you use a pool to start a target function, all necessary arguments to start the work will need to be serialized, regardless of of the start method or OS used. This is not the case for the lower-level Process class, where no such queues are used, and whether the arguments are pickled or not depends on the start method. This is also the reason why you can pass locks, queues and pipes as arguments to Process class, but not when using pools.
One last thing of note here is that when you do p.imap_unordered(comp.compute, compute_input), while you are not passing big_data as an argument explicitly, it will still be pickled since the target function is an instance method, and to start that you would need to pickle the instance attributes which includes big_data. Hence, when the target function is an instance method, all instance attributes are implicit arguments that will be put on queue by default. So, an alternate to Frank's answer could also be that you convert compute to a staticmethod, call it using type(comp).compute, and finally use big_data without storing it as an instance attribute at all.
Answered By - Charchit Agarwal Answer Checked By - Cary Denson (Python.Easybugfix Admin)
Post a Comment
Post a Comment