python - multiprocessing.Manager() communication with main programm didn't work -
why didn't work code? tried make process multiprocessing worked in while 1 loop. want these processes have shared memory main process comunicate together. demo-code testing manager:
import multiprocessing import time def f(ls,lso, ): print "input ls:" print ls print "input lso:" print lso ls[0] += 1 ls[1][0] += 1 ls_2 = ls[2] print ls_2 ls_2[0] += 1 print ls_2 ls[2] = ls_2 print "output ls:" print ls tmp = [] tmp.append([1, 2, 3]) tmp.append([5, 6, 7]) lso = tmp print "output lso:" print lso if __name__ == '__main__': manager = multiprocessing.manager() #ls = manager.list([1, [1], [1]]) ls = manager.list() lso = manager.list() lso = [[0, 0, 0], [0, 0, 0]] tmp = [] tmp.append(1) tmp.append([1]) tmp.append([1]) ls = tmp print 'before', ls, lso p = multiprocessing.process(target=f, args=(ls, lso, )) p.start() p.join() print 'after', ls, lso
the output is:
before [1, [1], [1]] [[0, 0, 0], [0, 0, 0]] after [1, [1], [1]] [[0, 0, 0], [0, 0, 0]]
once manager.list
object created, must set data inside object, opposed overriding it. if sprinkle print type(lso)
before , after lso=(value)
lines you'll see mean.
in following code 1 manager.list object given value @ creation time: ie: myvar = list([1,2,3])
. second created, data copied it: myvar = list(); myvar[:] = [2,3,4]
have fun!
source
import multiprocessing import time def f(ls,lso, ): print "input ls:" print ls print "input lso:" print lso ls[0] += 1 ls[1][0] += 1 ls_2 = ls[2] print ls_2 ls_2[0] += 1 print ls_2 ls[2] = ls_2 print "output ls:" print ls tmp = [] tmp.append([1, 2, 3]) tmp.append([5, 6, 7]) lso = tmp print "output lso:" print lso if __name__ == '__main__': manager = multiprocessing.manager() #ls = manager.list([1, [1], [1]]) ls = manager.list() lso = manager.list( [[0, 0, 0], [0, 0, 0]] ) tmp = [] tmp.append(1) tmp.append([1]) tmp.append([1]) ls[:] = tmp print 'before', ls, lso p = multiprocessing.process(target=f, args=(ls, lso, )) p.start() p.join() print 'after', ls, lso
output
before [1, [1], [1]] [[0, 0, 0], [0, 0, 0]] input ls: [1, [1], [1]] input lso: [[0, 0, 0], [0, 0, 0]] [1] [2] output ls: [2, [1], [2]] output lso: [[1, 2, 3], [5, 6, 7]] after [2, [1], [2]] [[0, 0, 0], [0, 0, 0]]
Comments
Post a Comment