1

I have written the following toy-code using multiprocessing in Python.

I want the function func to run in parallel and also write to the global variables: var, varr and varrr. The following code however only prints zeros for var, varr and varrr.

How do I make func modify the global variables var, varr and varrr???

import multiprocessing as mp
import itertools
import numpy as np

p = 3
ind = range(p)
ind_list = list(itertools.product(ind,ind,ind))

var = np.zeros([p,p,p])
varr = np.zeros([p,p,p])
varrr = np.zeros([p,p,p])

def func(ind_list):
    global var, varr, varrr
    i = ind_list[0]
    j = ind_list[1]
    k = ind_list[2]
    var[i][j] = i + j + k
    varr[i][j] = i*j*k
    varrr[i][j] = 2*i*j*k

for a in range(p):
    pool = mp.Pool()
    pool.map(func,ind_list)
    pool.close()
    pool.join()
    print('\n\n',var,'\n\n',varr,'\n\n',varrr,'\n\n')
Praful S
  • 11
  • 2

1 Answers1

0

As some comments already mentioned, multiprocessing does not share memory (i.e. you can't access global variables from within the processes). You can use pipes to share information for between processes.

Another option is to use Threading (which shares memory). However, this will technically not run completely in parallel due to the Global Interpreter lock.

Nik
  • 1
  • 1