Answer the question
In order to leave comments, you need to log in
How to pass complex objects between processes in python?
There is such task: It is
necessary to create and fill a certain hierarchy of classes, in different processes (for acceleration) and to return to the main process.
import multiprocessing
import multiprocessing.managers
class ChildClass:
name = "child name"
def __init__(self):
pass
class ParentClass:
name = "parent name"
child_class = []
def __init__(self):
num_of_childs = 3
for i in range(num_of_childs):
self.child_class.append(ChildClass())
def func(shared_array):
shared_array.append(ParentClass())
if __name__ == '__main__':
mngr = multiprocessing.Manager()
shared_array = mngr.list()
num_iterations = 3
processes = []
for i in range(3):
processes.append(multiprocessing.Process(target=func, args=(shared_array,)))
for process in processes:
process.start()
for process in processes:
process.join()
for obj in shared_array:
print(obj.name)
print(obj.child_class)
parent name
[]
parent name
[]
parent name
[]
Answer the question
In order to leave comments, you need to log in
Don't make yourself a problem, use threads, not processes.
Sharing data within one process is much easier than trying to do it between processes, not only in terms of logic and code, but also organization.
ps python and speed? These two words cannot stand side by side... and even if you need to use shared memory even more so, look for crap for yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question