在多处理过程之间共享状态

时间:2015-09-05 20:49:57

标签: python-3.x multiprocessing

这个documentation用于在进程之间共享状态,但它并没有完全回答我的问题。我有以下代码

from multiprocessing import Process, Value, Array

#This is my function
def my_func(my_array):
    my_array = ["hello", "hi", "howdy"]

# I am initiating an array called arr that has string elements here
if __name__ == '__main__':
    arr = Array('c')

    p = Process(target=my_func, args=(arr))
    p.start()
    p.join()

    print(arr[:])

我希望arr最后等于["hello", "hi", "howdy"],但我的代码不起作用。有谁知道为什么?

1 个答案:

答案 0 :(得分:0)

这不行。根据{{​​3}},允许的类型Array documentation

同样my_array从指向Array对象的指针变为指向my_func中字符串数组的指针。

解决这两个问题的更合适的方法是:

def my_fun(my_array):
  my_array[:] = map(ord, ['a','b','c'])

do not include strings将字符映射到整数

相关问题