Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

parallel processing - Using Python multiprocessing to run object computations

I need to do run some computations to update object attributes. I want to use parallel computing since I need to update multiple objects' attributes, that is, I have multiple objects and I need to do the same computation for each one. The objects do not share information between them.

I am currently using a process pool with map or a similar function, and the problem is that these processes copy the object, then do the computation, instead of just doing the computation directly using the original object. Is there any way around this?

As an example:

from multiprocessing import Pool

class A:

    def __init__(self, init):
        self.a = init

    def func(self, b):
        self.a = self.a + b

foo = A(2)
print(foo.a) # prints 2

p = Pool()
result = p.map(foo.func, (3,))
print(foo.a) #prints 2, should print 5

foo.func(3)
print(foo.a) #prints 5 as expected
question from:https://stackoverflow.com/questions/65931142/using-python-multiprocessing-to-run-object-computations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's my workaround using queues:

from multiprocessing import Process, Queue
import time

class A:
    def __init__(self, init):
        self.a = init

    def func(self, b):
        self.a = self.a + b

    def par_func(self, q, b):
        print('starting')
        self.func(b)
        q.put(self.a)
        time.sleep(10)
        print('whew! all done')

foo = A(2)
bar = A(6)
print('initial:', foo.a, bar.a) # prints 2, 6

q1, q2 = Queue(), Queue()
p1, p2 = Process(target=foo.par_func, args=(q1, 3)), Process(target=bar.par_func, args=(q2, 4))
p1.start()
p2.start()
p1.join()
p2.join()

#prints from par_func() were nonsequential i.e. parallelism is occurring 

foo.a = q1.get()
bar.a = q2.get()

print('final:', foo.a, bar.a) # prints 5, 10

this is probably far from ideal, but it works for my purposes. I'm leaving it here for anyone else with this problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...