0

I want to write a code that return counts is [1,2,3,4] while keeping the parallelized for loop. I'm trying to update the global array counts from the inside of increment function, but it never works and returns [0,0,0,0]. Is there any way than I can update the counts array from the increment function while parallel looping increment function?

import numpy as np
import multiprocessing as mpp
import timeit

start = timeit.default_timer()

num=5

counts=np.zeros((num,1))

def increment(k):
    global counts
    counts[k]=counts[k]+k

pool = mpp.Pool(4)
pool.map(increment,[k for k in range(0,num)])

print(counts)

stop = timeit.default_timer()

print('Time: ', stop - start) 
petezurich
  • 9,280
  • 9
  • 43
  • 57
tgg
  • 1
  • 1
    Does this answer your question? [multiprocessing global variable updates not returned to parent](https://stackoverflow.com/questions/11055303/multiprocessing-global-variable-updates-not-returned-to-parent) – michimo Dec 09 '19 at 07:05
  • You should use singleton class with no lock. But it is recommended not to use multiprocessing with these kind of processes: https://stackoverflow.com/a/1109930/12422518 – furkanayd Dec 09 '19 at 07:07

1 Answers1

0

Just return counts[k]+k and set what you want equal to increment(k)

Alec
  • 8,529
  • 8
  • 37
  • 63