Select to view content in your preferred language

How to parallelize functions within arcpy tools?

271
0
02-28-2024 02:19 PM
Labels (1)
uasdev
by
Occasional Contributor

Hi all,

I am unable to use parallel processing in a custom made python toolbox (.pyt). I have scoured the web and have not been able to find a solution. My tool iterates through a list of files, but I attached a test toolbox that follows the same structure.

How do I get this tool to run the num_list in parallel? Below is the .py file snippet.

Thanks in advance.

```

import arcpy
from multiprocessing import Pool

class SquareTool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Hello World"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        params = None
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return
       
       
    def square(self, num):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        squareResult = num * num
        arcpy.AddMessage(squareResult)
        return squareResult

    def execute(self, parameters, messages):
        """The source code of the tool."""  
        num_list = [0,1,2,3,4,5]
        pool = Pool()
        results = pool.map(self.square, num_list)
        arcpy.AddMessage(results)
        return

    def postExecute(self, parameters):
        """This method takes place after outputs are processed and
        added to the display."""
        return

```

0 Kudos
0 Replies