How to pass multiple values using Python Multiprocessing

121 views Asked by At

I have a script that will run through part of our company wide network to replace data when it is updated. The problem is I have hundred of paths and running it without multiprocessing takes all weekend. In the past I have broken similar scripts in to sub folders but it takes constant monitoring and running successive folders all weekend.

mappaths is a tuple containing the paths

def UpdateLayers(maps, oldNames, newLayer0, newLayer1, newLayer2, newLayer3, newLayer4, f):
    mxd = arcpy.mapping.MapDocument(maps)
    print maps
    for df in arcpy.mapping.ListDataFrames(mxd):
        for layer in arcpy.mapping.ListLayers(mxd, "", df):
            #Land Survey Base
            if layer.name in oldNames:
                if layer.name == oldNames[0]:
                    LayerReplace(layer, newLayer0, df, f, maps)
                elif layer.name == oldNames[1]:
                    LayerReplace(layer, newLayer1, df, f, maps)
                elif layer.name == oldNames[2]:
                    LayerReplace(layer, newLayer2, df, f, maps)
                elif layer.name == oldNames[3]:
                    LayerReplace(layer, newLayer3, df, f, maps)
                elif layer.name == oldNames[4]:
                    LayerReplace(layer, newLayer4, df, f, maps)
                else:
                    print "layer unable to replace"
    SaveMXD(f, mxd, maps)
    del mxd

##some more code

for maps in mappaths:
    UpdateLayers(maps, oldNames, newLayer0, newLayer1, newLayer2, newLayer3, newLayer4, f)

From what I have read on here, at some point I want to replace my for loop with something similar to

if __name__ == '__main__':
    p = multiprocessing.Pool()
    p.map(UpdateLayers, mappaths)

My problem is I don't know how to pass oldNames, newLayer0, newLayer1, newLayer2, newLayer3, newLayer4, fin to my function.

I figure what I am missing is fairly simple but programming and script writing is an additional part of my job just because I can usually figure it out.

1

There are 1 answers

0
khol On

From the docs:

map(func, iterable[, chunksize]) A parallel equivalent of the map() built-in function (it supports only one iterable argument though).

So, you will have to put the 8 arguments into a single list, pass that to UpdateLayers, then grab all the arguments from that list within the function.

Your call will look like:

p.map(UpdateLayers, [maps, oldNames, newLayer0, newLayer1, newLayer2, newLayer3, newLayer4, f])