@lawrenceyy wrote:
I just started exploring multiprocessing in python outside of ghpythonlib.parallel.run(). I've run into some unexpected results.
My test script:
import multiprocessing as mp import time from PIL import Image as image class myclass: def __init__(self,integer): self.integer=integer self.result=None self.image=None def func(_index): #this function uses PIL to generate a solid color image #the purpose is to cause significant CPU load myclasslist[_index].image=image.new("L",(2000,2000),0) temp=[myclasslist[_index].integer*20 for k in range(2000*2000)] myclasslist[_index].image.putdata(temp) #myclasslist[_index].result is what I am testing myclasslist[_index].result=myclasslist[_index].integer+100 print (myclasslist[_index].result) return myclasslist[_index].result time0=time.time() myclasslist=[myclass(i) for i in range(10)] pool=mp.Pool(processes=4) mpresult=pool.map(func,range(10)) time1=time.time() print(time1-time0) print (mpresults) print([myclasslist[i].result for i in range(10)])
In the end mpresults show the correct results, but myclasslist[i].result shows None. I read online that each process gets its own instance of my object, therefore when I do:
myclasslist[_index].result=myclasslist[_index].integer+100
it does not stick. Is there a way to get it to stick so when I print in the end it shows the correct values?And does anyone know how to get pool.map() to work with class methods()?
Posts: 1
Participants: 1