简体   繁体   English

与主程序的multiprocessing.Manager()通信不起作用

[英]multiprocessing.Manager() communication with main programm didn't work

Why didn't work this Code? 为什么该代码不起作用? I tried to make a Process with multiprocessing who worked in an while 1 loop. 我试图用多处理创建一个在while 1循环中工作的流程。 I want these Processes to have some shared memory with my main process to comunicate together. 我希望这些进程与我的主进程共享一些内存以进行通讯。 This is my Demo-Code for testing the Manager: 这是我用于测试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 a Manager.list object is created, you must set the data inside the object, as opposed to overriding it. 创建Manager.list对象后,必须在对象内部设置数据,而不是覆盖它。 If you sprinkle print type(lso) before and after the lso=(value) lines you'll see what I mean. 如果在lso=(value)行之前和之后撒上print type(lso) ,您会明白我的意思。

In the following code one Manager.list object is given a value at creation time: ie: myvar = list([1,2,3]) . 在以下代码中,在创建时为一个Manager.list对象提供了一个值:即: myvar = list([1,2,3]) The second is created, then data is copied into it: myvar = list(); myvar[:] = [2,3,4] 创建第二个,然后将数据复制到其中: myvar = list(); myvar[:] = [2,3,4] 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]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM