<?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 AddMessage not working in worker threads in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540134#M42213</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I developed a script for ArcGIS Pro which performs 4 heavy downloads. To speed-up the process, I delegated the download process to 4 worker threads and wait for them complete in order to continue the geoprocess operations. During the download process, I use the arcpy.AddMessage() function to give some feedback.&lt;/P&gt;&lt;P&gt;Everything works as expected when I execute de code as a python standalone script, including the feedback messages. However some messeges did not show when running the script in ArcGIS Pro.&lt;/P&gt;&lt;P&gt;I suspect that the worker threads are writing the messages in somewhere else. To test my thoughs, I wrote the following script:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;concurrent.futures
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;threading
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;arcpy


&lt;SPAN style="color: #cc7832;"&gt;def &lt;/SPAN&gt;&lt;SPAN style="color: #ffc66d;"&gt;add_message_wrapper&lt;/SPAN&gt;(message):
    arcpy.AddMessage(&lt;SPAN style="color: #6a8759;"&gt;'Thread {0} says: {1}'&lt;/SPAN&gt;.format(threading.current_thread().getName()&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;message))


&lt;SPAN style="color: #cc7832;"&gt;if &lt;/SPAN&gt;__name__ == &lt;SPAN style="color: #6a8759;"&gt;'__main__'&lt;/SPAN&gt;:
    add_message_wrapper(&lt;SPAN style="color: #6a8759;"&gt;'Hello addMessage'&lt;/SPAN&gt;)
    &lt;SPAN style="color: #cc7832;"&gt;with &lt;/SPAN&gt;concurrent.futures.ThreadPoolExecutor() &lt;SPAN style="color: #cc7832;"&gt;as &lt;/SPAN&gt;executor:
        futures = [executor.submit(add_message_wrapper&lt;SPAN style="color: #cc7832;"&gt;,
&lt;/SPAN&gt;&lt;SPAN style="color: #6a8759;"&gt;                   'Hello addMessage from a worker thread'&lt;/SPAN&gt;) &lt;SPAN style="color: #cc7832;"&gt;for &lt;/SPAN&gt;i &lt;SPAN style="color: #cc7832;"&gt;in &lt;/SPAN&gt;&lt;SPAN style="color: #8888c6;"&gt;range&lt;/SPAN&gt;(&lt;SPAN style="color: #6897bb;"&gt;6&lt;/SPAN&gt;)]
        &lt;SPAN style="color: #cc7832;"&gt;for &lt;/SPAN&gt;future &lt;SPAN style="color: #cc7832;"&gt;in &lt;/SPAN&gt;concurrent.futures.as_completed(futures):
            &lt;SPAN style="color: #cc7832;"&gt;pass
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;        &lt;/SPAN&gt;add_message_wrapper(&lt;SPAN style="color: #6a8759;"&gt;'Back to main...'&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;The&amp;nbsp;output as a standalone python script:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #999999;"&gt;Thread MainThread says: Hello addMessage&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_1 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_2 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread MainThread says: Back to main...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;The&amp;nbsp; output as a script in ArcGIS Pro:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Messages
Start Time: sexta-feira, 1 de fevereiro de 2019 11:49:40
Running script testAddMessage...
Thread Dummy-1 says: Hello addMessage
Thread Dummy-1 says: Back to main...
Completed script testAddMessage...
Succeeded at sexta-feira, 1 de fevereiro de 2019 11:49:45 (Elapsed Time: 4,80 seconds)&lt;/PRE&gt;&lt;P&gt;So, how can I get the messages from worker threads sent to ArcGIS Pro output?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 23:25:42 GMT</pubDate>
    <dc:creator>PlinioSantos</dc:creator>
    <dc:date>2021-12-11T23:25:42Z</dc:date>
    <item>
      <title>AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540134#M42213</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I developed a script for ArcGIS Pro which performs 4 heavy downloads. To speed-up the process, I delegated the download process to 4 worker threads and wait for them complete in order to continue the geoprocess operations. During the download process, I use the arcpy.AddMessage() function to give some feedback.&lt;/P&gt;&lt;P&gt;Everything works as expected when I execute de code as a python standalone script, including the feedback messages. However some messeges did not show when running the script in ArcGIS Pro.&lt;/P&gt;&lt;P&gt;I suspect that the worker threads are writing the messages in somewhere else. To test my thoughs, I wrote the following script:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;concurrent.futures
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;threading
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;arcpy


&lt;SPAN style="color: #cc7832;"&gt;def &lt;/SPAN&gt;&lt;SPAN style="color: #ffc66d;"&gt;add_message_wrapper&lt;/SPAN&gt;(message):
    arcpy.AddMessage(&lt;SPAN style="color: #6a8759;"&gt;'Thread {0} says: {1}'&lt;/SPAN&gt;.format(threading.current_thread().getName()&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;message))


&lt;SPAN style="color: #cc7832;"&gt;if &lt;/SPAN&gt;__name__ == &lt;SPAN style="color: #6a8759;"&gt;'__main__'&lt;/SPAN&gt;:
    add_message_wrapper(&lt;SPAN style="color: #6a8759;"&gt;'Hello addMessage'&lt;/SPAN&gt;)
    &lt;SPAN style="color: #cc7832;"&gt;with &lt;/SPAN&gt;concurrent.futures.ThreadPoolExecutor() &lt;SPAN style="color: #cc7832;"&gt;as &lt;/SPAN&gt;executor:
        futures = [executor.submit(add_message_wrapper&lt;SPAN style="color: #cc7832;"&gt;,
&lt;/SPAN&gt;&lt;SPAN style="color: #6a8759;"&gt;                   'Hello addMessage from a worker thread'&lt;/SPAN&gt;) &lt;SPAN style="color: #cc7832;"&gt;for &lt;/SPAN&gt;i &lt;SPAN style="color: #cc7832;"&gt;in &lt;/SPAN&gt;&lt;SPAN style="color: #8888c6;"&gt;range&lt;/SPAN&gt;(&lt;SPAN style="color: #6897bb;"&gt;6&lt;/SPAN&gt;)]
        &lt;SPAN style="color: #cc7832;"&gt;for &lt;/SPAN&gt;future &lt;SPAN style="color: #cc7832;"&gt;in &lt;/SPAN&gt;concurrent.futures.as_completed(futures):
            &lt;SPAN style="color: #cc7832;"&gt;pass
&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;        &lt;/SPAN&gt;add_message_wrapper(&lt;SPAN style="color: #6a8759;"&gt;'Back to main...'&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;P&gt;The&amp;nbsp;output as a standalone python script:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #999999;"&gt;Thread MainThread says: Hello addMessage&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_1 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_0 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread ThreadPoolExecutor-0_2 says: Hello addMessage from a worker thread&lt;/SPAN&gt;&lt;BR style="color: #3d3d3d;" /&gt;&lt;SPAN style="color: #999999;"&gt;Thread MainThread says: Back to main...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;The&amp;nbsp; output as a script in ArcGIS Pro:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;Messages
Start Time: sexta-feira, 1 de fevereiro de 2019 11:49:40
Running script testAddMessage...
Thread Dummy-1 says: Hello addMessage
Thread Dummy-1 says: Back to main...
Completed script testAddMessage...
Succeeded at sexta-feira, 1 de fevereiro de 2019 11:49:45 (Elapsed Time: 4,80 seconds)&lt;/PRE&gt;&lt;P&gt;So, how can I get the messages from worker threads sent to ArcGIS Pro output?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:25:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540134#M42213</guid>
      <dc:creator>PlinioSantos</dc:creator>
      <dc:date>2021-12-11T23:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540135#M42214</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;probably just printing... build your message string and 'tweet' it&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;tweet&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;msg&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="string token"&gt;"""Print a message for both arcpy and python.

    msg - a text message
    """&lt;/SPAN&gt;
    m &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"\n{}\n"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;msg&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddMessage&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;m&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;m&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:25:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540135#M42214</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T23:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540136#M42215</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan, your solution duplicate the messages in python console (stand alone python script) and yet did not makes ArcGIS Pro output to show messages sent by a worker thread.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:34:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540136#M42215</guid>
      <dc:creator>PlinioSantos</dc:creator>
      <dc:date>2019-02-01T18:34:37Z</dc:date>
    </item>
    <item>
      <title>Re: AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540137#M42216</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;wouldn't your worker scripts would need to return the messages to the main script then? or are your workers isolated once they are called?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Feb 2019 18:51:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540137#M42216</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2019-02-01T18:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540138#M42217</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;All code are in the same python module, i.e., same script, just as the snippet in the main post. The concurrent.futures module starts worker threads to execute a funcion (heavy network operations in my real case) and the concurrent.futures.as_completed yelds the results as soon as the worker thread execution ends. My problem is that&amp;nbsp;only&amp;nbsp;messages sent from the starter thread&amp;nbsp;are visible on ArcGIS Pro interface.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Feb 2019 19:02:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/540138#M42217</guid>
      <dc:creator>PlinioSantos</dc:creator>
      <dc:date>2019-02-01T19:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: AddMessage not working in worker threads</title>
      <link>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/1535495#M72788</link>
      <description>&lt;P&gt;I have the same problem, did you ever find a solution for this?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Sep 2024 14:29:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/addmessage-not-working-in-worker-threads/m-p/1535495#M72788</guid>
      <dc:creator>SarahGe</dc:creator>
      <dc:date>2024-09-05T14:29:20Z</dc:date>
    </item>
  </channel>
</rss>

