开发者

Changing "global" array with pool processes

开发者 https://www.devze.com 2023-02-26 02:58 出处:网络
Apologies first for the small code dump to follow, but I\'ve spent the evening taking my first baby steps into the multiprocessing module; with my previous knowledge coming from openMPI fortran. I\'m

Apologies first for the small code dump to follow, but I've spent the evening taking my first baby steps into the multiprocessing module; with my previous knowledge coming from openMPI fortran. I'm having issues with the below code in that although all the threads spawn and run etc and have access to all the correct values (both global and local) the changes each inacts upon the numpy points array do not remain. I have tried both setting the array as "global" as well as segregating into a separate class to access it both to no avail. I guess I'm missing some fundamental understanding.

Important parts:

po开发者_如何学Pythonints = np.ones( N )

def explore(pos,rad):
    #find range of points for comparison
    low = []
    high = []
    for dim in pos:
        low.append( int( floor( (dim - rad - 0.5*radius) / radius ) ) )
        high.append( int( ceil( (dim + rad + 0.5*radius) / radius ) ) )

    #check for overlap
    for x in xrange(low[0],high[0]+1):
        for y in xrange(low[1],high[1]+1):
            for z in xrange(low[2],high[2]+1):
                if points[x%N[0],y%N[1],z%N[2]]:
                    point = (x*radius,y*radius,z*radius)
                    distance = (point[0]-pos[0])**2 + (point[1]-pos[1])**2 + (point[2]-pos[2])**2 
                    if distance <= (rad+(0.5*radius))**2:
                        points[x%N[0],y%N[1],z%N[2]] = 0
    return

pool = Pool()
for i in xrange( atoms ):
    pos = ...
    rad = ...
    pool.apply_async(explore,(pos,rad,))
pool.close()
pool.join()  


If you're using multiprocessing.pool, it's not threads, it's full processes you're spawning here. The doc says:

The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads.

EDIT: Alternatively, there's undocumented multiprocessing.pool.ThreadPool, with same API as multiprocessing.pool. But yes, it's undocumented, which means it might break/go away etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消