python multiprocessing: processes don't work -
i want assign computing jobs more 1 cpu, choose multiprocessing. however, result not want.
import numpy np multiprocessing import process def func(begin,end): print('*'*5) print('begin=%d' %(begin)) in range(begin,end): j in range(10): myarray[i][j]=1 myarray=np.zeros((12,10)) print(myarray) in range(4): begin=i*3 end=(i+1)*3 p=process(target=func,args=(begin,end,)) p.start() print('*'*5) print(myarray)
i think myarray should ones. doesn't change @ all. why? func function not change elements of myarray? tried example linkenter link description here
from multiprocessing import process def f(name): print('hello',name) p=process(target=f,args=('bob',)) p.start()
it shows nothing on screen. why? how should finish computation python? can give way of take advantage of multi-cpus?
there 2 problems there:
when print array @ end, how know processes have finished? need invoke
join()
on each process ensure have finished.each process has copy of "myarray". if want communicate several processes need either use
queue
orpipe
. check the documentation talks exchanging data between processes
here working example using base 1 posted (it not intended fast, show how communication done):
from multiprocessing import process, freeze_support, queue def func(my_id, q, begin, end): global myarray print('process %d has range: %d - %d' % (my_id, begin, end)) in range(begin,end): q.put((i, * 2)) if __name__ == "__main__": freeze_support() q = queue() processes = [] myarray=[0] * 12 print("at beginning array ", myarray) in range(4): begin = i*3 end = (i+1)*3 p = process(target=func, args=(i, q, begin, end)) p.start() processes.append(p) p in processes: p.join() while not q.empty(): (index, value) = q.get() myarray[index] = value print("at end array ", myarray)
try change line p.join()
pass
see happens :)
Comments
Post a Comment