<?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: Using multiple CPUs in Python script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1113505#M62879</link>
    <description>&lt;P&gt;Hi Don, thanks for the pseudo code and the hint about potential naming conflicts with temporary feature classes. I will try it out. So far I have no intention to develop a python toolbox, a script should be perfect for my purposes.&lt;/P&gt;</description>
    <pubDate>Wed, 03 Nov 2021 06:14:46 GMT</pubDate>
    <dc:creator>CarstenSchuermann</dc:creator>
    <dc:date>2021-11-03T06:14:46Z</dc:date>
    <item>
      <title>Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112675#M62847</link>
      <description>&lt;P&gt;Hi, I have developed a Python script that iterates through a list of feature classes, in order to apply the same series of geoprocessing tools upon each feature class. In the script, I implemented it like this:&lt;/P&gt;&lt;P&gt;fclist = ("fc1","fc2","fc3", ...")&lt;/P&gt;&lt;P&gt;for i in fclist:&lt;/P&gt;&lt;P&gt;&amp;nbsp;arcpy.management.AddField(i, ...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;arcpy.management.CalculateField(i, ...)&lt;/P&gt;&lt;P&gt;&amp;nbsp;...&lt;/P&gt;&lt;P&gt;It works fine, but I am wondering whether it is possible to speed up processing time by telling Pythin to use not just one but multiple CPUs (for example, use 50% of the available CPUs), i.e. how can I tell Python to perform the "for" loop in parallel?&lt;/P&gt;&lt;P&gt;As the result of the "for" loop for one feature class is not input to or dependend on the results of the other feature classes, I think parallel processing should be possible.&lt;/P&gt;&lt;P&gt;All feature classes are located in the same feature datasets, so I am furthermore wondering whether I am running into "system lock" problems if I am going to parallise this processing?&lt;/P&gt;&lt;P&gt;I appreciate of someone could share his experiences with multi-core / multip-CPU processing and could provide some code snippets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 07:52:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112675#M62847</guid>
      <dc:creator>CarstenSchuermann</dc:creator>
      <dc:date>2021-10-31T07:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112680#M62848</link>
      <description>&lt;P&gt;Did you see the documentation on cpu and gpu support?&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/environment-settings/processor-type.htm" target="_blank"&gt;Processor Type (Environment setting)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/environment-settings/parallel-processing-factor.htm" target="_blank"&gt;Parallel Processing Factor (Environment setting)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/classes/env.htm" target="_blank"&gt;env—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 10:55:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112680#M62848</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-10-31T10:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112684#M62849</link>
      <description>&lt;P&gt;I've done quite a lot of this and from what you describe it should work with no problem using the python multiprocessing package. The pseudocode below shows the basic flow.&amp;nbsp; As long as each process only touches its own feature class there should be no locking problem.&amp;nbsp; My computer has 8 threads and I usually max it out at 8, there is some odd pleasure in seeing the CPU meters all running at 100%.&amp;nbsp; Throughput increases typically about 5 times what you get with a single processor. Even at 100% I can still use my system to do lightweight interactive work (eg. browsing web sites).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&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;import multiprocessing

p = multiprocessing.Pool(&amp;lt;number of processors&amp;gt;)
p.map(&amp;lt;target function&amp;gt;, &amp;lt;list of feature class names&amp;gt;)
p.close() 

def &amp;lt;target_function&amp;gt; (feature_class_name):
    ... your code to process one feature class
    return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;One final note - I HAVE had some problems to get this to work running in a python toolbox, but no problem in a standalone script.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And another final note - you need to be careful with naming conflicts if you are creating temporary feature classes as part of the processing.&amp;nbsp; I usually solve this by appending something like the process ID to the name of the temporary object&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 12:37:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112684#M62849</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2021-10-31T12:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112688#M62850</link>
      <description>&lt;P&gt;Mine is similar to DonMorrisons, but also assigns the pythonw.exe to use and returns some info from the tasks:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def function_To_Process(fc, etc args):
    try:
        # Do stuff to the fc
        return {'result': True}

    except Exception as ex:
        return { 'result': False, 'error': ex}

if __name__ == '__main__':
    ctx = multiprocessing.get_context('spawn')
    ctx.set_executable(r'path to your pythonw.exe')
    
    # create list of fc's/args etc that you want processed
    jobs = [(fc1,args), (fc2, args), (fc3, args), etc...)]

    cpuNum = ctx.cpu_count()
    # throttle cpu to 80% if wanted
    # cpuNum = int(math.ceil(cpuNum * (80 / 100)))

    with ctx.Pool(processes=cpuNum) as pool:
        res = pool.starmap(function_To_Process, jobs)

    if len(res) &amp;gt; 0:
        for result in res:
            print(f'Failed: {result.get("error", "None this time")}')&lt;/LI-CODE&gt;&lt;P&gt;r&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 13:33:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1112688#M62850</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-10-31T13:33:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1113505#M62879</link>
      <description>&lt;P&gt;Hi Don, thanks for the pseudo code and the hint about potential naming conflicts with temporary feature classes. I will try it out. So far I have no intention to develop a python toolbox, a script should be perfect for my purposes.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 06:14:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1113505#M62879</guid>
      <dc:creator>CarstenSchuermann</dc:creator>
      <dc:date>2021-11-03T06:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using multiple CPUs in Python script</title>
      <link>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1113507#M62880</link>
      <description>&lt;P&gt;Thank you for the additional remarks, Jeff.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Nov 2021 06:16:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-multiple-cpus-in-python-script/m-p/1113507#M62880</guid>
      <dc:creator>CarstenSchuermann</dc:creator>
      <dc:date>2021-11-03T06:16:41Z</dc:date>
    </item>
  </channel>
</rss>

