I didn't have a report on this, so I'll drop a note.
In Python, the standard module Multiprocessing allows parallel computing. Hardware parallel computing like GPU is not supported, it is just multi-process processing on CPU.
The elements of the pooled argument list are passed to the arguments of the function you want to process in parallel.
If you write a script that refers to a global variable in a function that is processed in parallel, it will be as follows.
example.py
import multiprocessing as mp
a = 0
def init_global():
global a
a = 100
def func(proc):
global a
print("Inside MultiProcess: ", proc, a)
return a
if __name__ == "__main__":
print("Before Change", a)
init_global()
print("After Change", a)
pool = mp.Pool(4)
callback = pool.map(func, range(4))
pool.terminate()
pool.join()
print(sum(callback))
result As you can see, you can refer to and assign in a normal function (init_global), but you cannot refer to a global variable in a function that is processed in parallel.
If you really want to refer to the value of a global variable in a parallel processing function, you have to specify the argument from the caller. (If you want to pass multiple arguments, you need to generate a tuple)
example.py
import multiprocessing as mp
a = 0
b = 1111
def init_global():
global a
a = 100
def func(proc):
print("Inside MultiProcess: ", proc[0], proc[1])
return proc[0]
if __name__ == "__main__":
print("Before Change", a)
init_global()
print("After Change", a)
pool = mp.Pool(4)
args = [(a,b) for i in range(4)]
callback = pool.map(func, args)
pool.terminate()
pool.join()
print(sum(callback))
result I was able to hand it over.
There may be a proper method, but I couldn't find it, so please report if there is a better method.