Here is sample code of Sharing state between processes
from multiprocessing import Process, Value, Array
def f(n, a):
n.value = 3.1415927
for i in range(len(a)):
a[i] = -a[i]
if __name__ == '__main__':
num = Value('d', 0.0)
arr = Array('i', range(10))
p = Process(target=f, args=(num, arr))
p.start()
p.join()
print(num.value)
print(arr[:])
The output is
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
I want to initialize a list with string elements instead of integer elements. Then I want to assign the list specific string elements. My code is the following.
from multiprocessing import Process, Value, Array
def f(a):
a = ["up", "down", "left"]
if __name__ == '__main__':
arr = Array('b', [])
p = Process(target=f, args=(arr))
p.start()
p.join()
print(arr[:])
I want the output to be
["up", "down", "left"]
But instead I get the output
TypeError: f() missing 1 required positional argument: 'a'
[]