Feb-02-2022, 01:10 PM
(This post was last modified: Feb-02-2022, 01:36 PM by deanhystad.)
This should work on linux, but it will not work on windows. In linux a process is forked, starting out with the same process image as the parent. In windows a process is spawned, starting out as a completely new process.
To have a variable that is the same for all processes, you need to pass it as an argument to the process.
From the docs: https://docs.python.org/3/library/multiprocessing.html
To have a variable that is the same for all processes, you need to pass it as an argument to the process.
From the docs: https://docs.python.org/3/library/multiprocessing.html
from multiprocessing import Process, Value, Array #Data can be stored in a shared memory map using Value or Array. For example, the following code 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[:])Or you can use a process manager. From the same document.
from multiprocessing import Process, Manager def f(d, l): d[1] = '1' d['2'] = 2 d[0.25] = None l.reverse() if __name__ == '__main__': with Manager() as manager: d = manager.dict() l = manager.list(range(10)) p = Process(target=f, args=(d, l)) p.start() p.join() print(d) print(l)