<?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 MapDocument and Threading in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/mapdocument-and-threading/m-p/86754#M6797</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm having problems with getting the mxd from the threaded function call, it's looks like it's an instance I cannot access.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My task is to loop through the mxd paths, open the mxd, loop the layers and re-point to designated datasources, loop the tables and re-point, save the mxd as another one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reason I'm trying to access the mxd created in the threaded process:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I have a batch script that is processing a lot of MXDs and there are mxds that simply &lt;STRONG&gt;crashes the whole process&lt;/STRONG&gt;, bypassing the try: catch: blocks. Don't ask me why, but they fail to open in ArcMap as well. Not really my issue, all I want is to make sure it won't crash the process.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why don't I put the whole process of opening the mxd and looping via datasources in the threaded call&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I've noticed that once I create an mxd in the threaded call, I can list the layers, but the subsequent call to list the tableviews fails with the&amp;nbsp;"&lt;STRONG&gt;'NoneType' object has no attribute '_arc_object'&lt;/STRONG&gt;" &lt;BR /&gt;So I suspect there is something behind the scenes in the MapDocument in arcpy that is not playing nice with the threading.&lt;BR /&gt;If I do the same thing in the non-threaded script for a valid mxd it won't fail on list layer and subsequent list tables&lt;BR /&gt;If I do the same thing in the threaded script for a valid mxd, it will fail on listing the tables. If I omit listing the layers, listing the tables will work. weird huh?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So what am I trying to do&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I wanted to access the mxd in the threaded process so if it is to crash the process, it won't disrupt the main thread. If it opens ok, I want to access this thread's instance of MXD in the main thread.&amp;nbsp;&lt;STRONG&gt;Is this possible?&amp;nbsp;&lt;/STRONG&gt;If so,&amp;nbsp;&lt;STRONG&gt;how?&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;I'm also unable to provide a pointer only that I would be able to modify within the threaded process (via class with the Thread parent class)&lt;/LI&gt;&lt;LI&gt;I'm also unable to modify the global variable from within the threaded process.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One of the (and simplest) approaches I've done is the following, which returns whatever you see in the screenshot capture:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;from &lt;/SPAN&gt;multiprocessing.pool &lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;ThreadPool
pool = ThreadPool(&lt;SPAN style="color: #660099;"&gt;processes&lt;/SPAN&gt;=&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;)
mxd = pool.apply(openMapDocument, (mxd_path,))&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;and...&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;def &lt;/SPAN&gt;openMapDocument(mxd_path):
&lt;SPAN style="color: #808080;"&gt;mxd &lt;/SPAN&gt;= &lt;SPAN style="color: #000080;"&gt;None
&lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;try&lt;/SPAN&gt;:
        mxd = arcpy.mapping.MapDocument(mxd_path)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;return &lt;/SPAN&gt;mxd

&lt;SPAN style="color: #000080; font-weight: bold;"&gt;except &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;BaseException &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;as &lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;ex&lt;/SPAN&gt;:
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print &lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;'Unable to open map document'
&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;        &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;raise&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;IMG alt="This will then fail on listing anything from the document with the AttributeError: " nonetype="" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 23:19:28 GMT</pubDate>
    <dc:creator>MichalGasparovic</dc:creator>
    <dc:date>2021-12-10T23:19:28Z</dc:date>
    <item>
      <title>MapDocument and Threading</title>
      <link>https://community.esri.com/t5/python-questions/mapdocument-and-threading/m-p/86754#M6797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm having problems with getting the mxd from the threaded function call, it's looks like it's an instance I cannot access.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My task is to loop through the mxd paths, open the mxd, loop the layers and re-point to designated datasources, loop the tables and re-point, save the mxd as another one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Reason I'm trying to access the mxd created in the threaded process:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I have a batch script that is processing a lot of MXDs and there are mxds that simply &lt;STRONG&gt;crashes the whole process&lt;/STRONG&gt;, bypassing the try: catch: blocks. Don't ask me why, but they fail to open in ArcMap as well. Not really my issue, all I want is to make sure it won't crash the process.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why don't I put the whole process of opening the mxd and looping via datasources in the threaded call&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I've noticed that once I create an mxd in the threaded call, I can list the layers, but the subsequent call to list the tableviews fails with the&amp;nbsp;"&lt;STRONG&gt;'NoneType' object has no attribute '_arc_object'&lt;/STRONG&gt;" &lt;BR /&gt;So I suspect there is something behind the scenes in the MapDocument in arcpy that is not playing nice with the threading.&lt;BR /&gt;If I do the same thing in the non-threaded script for a valid mxd it won't fail on list layer and subsequent list tables&lt;BR /&gt;If I do the same thing in the threaded script for a valid mxd, it will fail on listing the tables. If I omit listing the layers, listing the tables will work. weird huh?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So what am I trying to do&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I wanted to access the mxd in the threaded process so if it is to crash the process, it won't disrupt the main thread. If it opens ok, I want to access this thread's instance of MXD in the main thread.&amp;nbsp;&lt;STRONG&gt;Is this possible?&amp;nbsp;&lt;/STRONG&gt;If so,&amp;nbsp;&lt;STRONG&gt;how?&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;I'm also unable to provide a pointer only that I would be able to modify within the threaded process (via class with the Thread parent class)&lt;/LI&gt;&lt;LI&gt;I'm also unable to modify the global variable from within the threaded process.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One of the (and simplest) approaches I've done is the following, which returns whatever you see in the screenshot capture:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;from &lt;/SPAN&gt;multiprocessing.pool &lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;ThreadPool
pool = ThreadPool(&lt;SPAN style="color: #660099;"&gt;processes&lt;/SPAN&gt;=&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;)
mxd = pool.apply(openMapDocument, (mxd_path,))&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;and...&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;def &lt;/SPAN&gt;openMapDocument(mxd_path):
&lt;SPAN style="color: #808080;"&gt;mxd &lt;/SPAN&gt;= &lt;SPAN style="color: #000080;"&gt;None
&lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;try&lt;/SPAN&gt;:
        mxd = arcpy.mapping.MapDocument(mxd_path)
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;return &lt;/SPAN&gt;mxd

&lt;SPAN style="color: #000080; font-weight: bold;"&gt;except &lt;/SPAN&gt;&lt;SPAN style="color: #000080;"&gt;BaseException &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;as &lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;ex&lt;/SPAN&gt;:
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;print &lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;'Unable to open map document'
&lt;/SPAN&gt;&lt;SPAN style="color: #008000; font-weight: bold;"&gt;        &lt;/SPAN&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;raise&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;IMG alt="This will then fail on listing anything from the document with the AttributeError: " nonetype="" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:19:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/mapdocument-and-threading/m-p/86754#M6797</guid>
      <dc:creator>MichalGasparovic</dc:creator>
      <dc:date>2021-12-10T23:19:28Z</dc:date>
    </item>
  </channel>
</rss>

