<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python multiprocessing module in toolbox in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1367857#M69588</link>
    <description>&lt;P&gt;Thanks StaticK. It is a shame ESRI haven't streamlined some of this via arcpy&amp;nbsp; - especially considering how powerful it'd be in an arcpy toolbox.&lt;/P&gt;</description>
    <pubDate>Tue, 09 Jan 2024 14:36:44 GMT</pubDate>
    <dc:creator>LewisTrotter</dc:creator>
    <dc:date>2024-01-09T14:36:44Z</dc:date>
    <item>
      <title>Python multiprocessing module in toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1339104#M68983</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am trying to get some basic code that uses the multiprocessing module to work in a Pro 3.1 toolbox but it just won't play nice. The code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import time
import arcpy
import multiprocessing as mp

def work_log(work_data):
    arcpy.AddMessage(" Process %s waiting %s seconds" % (work_data[0], work_data[1]))
    time.sleep(int(work_data[1]))
    arcpy.AddMessage(" Process %s Finished." % work_data[0])

def pool_handler(work):
    p = mp.Pool(2)
    p.map(work_log, work)

if __name__ == '__main__':
    work = (["A", 5], ["B", 2], ["C", 1], ["D", 3])
    pool_handler(work)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I run this in an .atnx or .pyt I get the same error:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;PicklingError: Can't pickle &amp;lt;function work_log at 0x00000134B287D430&amp;gt;: attribute lookup work_log on __main__ failed&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also get 2 new instances of Pro open automatically if I run in a .pyt toolbox, I assume due to lack of __main__.&lt;/P&gt;&lt;P&gt;Does anyone have any advice on how to get something like this running in a Pro toolbox (preferably .pyt)?&lt;/P&gt;&lt;P&gt;I came across a similar question here&amp;nbsp;&lt;A href="https://community.esri.com/t5/python-questions/using-multiprocessing/m-p/1280108" target="_blank" rel="noopener"&gt;https://community.esri.com/t5/python-questions/using-multiprocessing/m-p/1280108&lt;/A&gt;&amp;nbsp;but it has never been answered.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 13:32:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1339104#M68983</guid>
      <dc:creator>LewisTrotter</dc:creator>
      <dc:date>2023-10-18T13:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python multiprocessing module in toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1339403#M68990</link>
      <description>&lt;P&gt;The multiprocessing module has a lot of caveats due to how Pro manages its Python environment. &lt;A href="https://gis.stackexchange.com/a/140777" target="_self"&gt;This link&lt;/A&gt; has a lot advice and some sample scripts, this might be enough to get you started.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Oct 2023 22:26:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1339403#M68990</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2023-10-18T22:26:29Z</dc:date>
    </item>
    <item>
      <title>Re: Python multiprocessing module in toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1345926#M69158</link>
      <description>&lt;P&gt;Not sure if it's possible within a pyt due to the nature of the geprocessing environment loading the pyt classes, the __main__ requirement. As a script tool- this would be the script named MultiProc.py and referenced (not embedded) in the script tool. The first Parameter is a value table, sting int.&lt;/P&gt;&lt;P&gt;The messages wont display from the thread until the threads are done so you can put them in the result dictionary to print them all.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import time
import arcpy
import multiprocessing as mp
import os
import sys

mp.set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))

def work_log(work_data):
    res_dict = {'process': f"Process {work_data[0]} waiting {work_data[1]} seconds", 'result': ''}
    time.sleep(work_data[1])
    res_dict['result'] = f"Process {work_data[0]} Finished."

    return res_dict

if __name__ == "__main__":
    import Multiproc

    work_table = arcpy.GetParameterAsText(0)

    # Create a value table with 2 columns
    value_table = arcpy.ValueTable(2)

    # Set the values of the table with the contents of the first argument
    if work_table:
        value_table.loadFromString(arcpy.GetParameterAsText(0))
    else:
        for i in [["A", 5], ["B", 2], ["C", 1], ["D", 3]]:
            value_table.addRow(i)

    pairs = []
    # Loop through the list of inputs
    for i in range(0, value_table.rowCount):
        pairs.append([value_table.getValue(i, 0), int(value_table.getValue(i, 1))])

    arcpy.AddMessage(pairs)

    with mp.Pool(2) as pool:
        jobs = [pool.apply_async(Multiproc.work_log, (pair, )) for pair in pairs]
        res = [j.get() for j in jobs]

        for r in res:
            arcpy.AddMessage(f"{r['process']} : {r['result']}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Nov 2023 16:17:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1345926#M69158</guid>
      <dc:creator>StaticK</dc:creator>
      <dc:date>2023-11-05T16:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python multiprocessing module in toolbox</title>
      <link>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1367857#M69588</link>
      <description>&lt;P&gt;Thanks StaticK. It is a shame ESRI haven't streamlined some of this via arcpy&amp;nbsp; - especially considering how powerful it'd be in an arcpy toolbox.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2024 14:36:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-multiprocessing-module-in-toolbox/m-p/1367857#M69588</guid>
      <dc:creator>LewisTrotter</dc:creator>
      <dc:date>2024-01-09T14:36:44Z</dc:date>
    </item>
  </channel>
</rss>

