<?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: Fault Taulerant Python Services -- arcpy in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676279#M52374</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah, I went down that route already. The problem is that it fails on import so "arcpy" doesn't exist so there's nothing to reload.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_1447704503936666" data-renderedposition="50_8_912_16" jivemacro_uid="_1447704503936666"&gt;&lt;P&gt;reload(arcpy)&lt;/P&gt;&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;NameError: name 'arcpy' is not defined&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It seems to be something cached at a non-python level.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 16 Nov 2015 20:12:46 GMT</pubDate>
    <dc:creator>JasonTipton</dc:creator>
    <dc:date>2015-11-16T20:12:46Z</dc:date>
    <item>
      <title>Fault Tolerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676274#M52369</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have Windows Services that need to run 24x7 (except for reboots). They use the arcpy package and a Floating License. I have run into issues with the service obtaining a license. For example, the license manager might be down.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I need is to try and import arcpy. If that fails, keep trying every x amount of seconds until it is successful. So, you would think something like this works:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy_imported = False
while arcpy_imported is False:
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy_imported = True
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as ex:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print ex
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(2)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But it does not. You can import arcpy all day long and if it fails the 1st time, it will fail infinitely. I tested this by disconnecting from the network, running "import arcpy" a few times, then connect back to the network and running "import arcpy" a few times.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Traceback:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 21, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from arcpy.geoprocessing import gp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\__init__.py", line 14, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; from _base import *&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 592, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; env = GPEnvironments(gp)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 589, in GPEnvironments&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return GPEnvironment(geoprocessor)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 545, in __init__&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self._refresh()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 547, in _refresh&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; envset = (set(env for env in self._gp.listEnvironments()))&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;RuntimeError: NotInitialized&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It would never succeed with the same python session open. I could open a separate session, and successfully import arcpy, come back to the failing session, and it continues to fail.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This means, that when the LM goes down for whatever reason, no amount of error handling can keep my services running without manually restarting them. Well, OK, I could write something to monitor these services and automatically restart them when this problem arises, but I would rather handle it within the actual service.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:30:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676274#M52369</guid>
      <dc:creator>JasonTipton</dc:creator>
      <dc:date>2021-12-12T04:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Fault Taulerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676275#M52370</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;So, this seems ridiculous, but what I have so far is to make a seperate module (let's say foo.py for now) and all it will do is to import arcpy.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then, call that module by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;while subprocess.call(r'python.exe \\path\to\foo.py') &amp;lt;&amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass
import arcpy&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will prevent the code from loading arcpy until another process is able to load arcpy. When it finally succeeds, it the process will return 0 and exit the loop. At that point, we assume that it is safe to proceed and then we can import arcpy in our own code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I would really like to handle this a little better.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;EDIT:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am marking this answer as correct until somebody comes up with a better working solution.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:30:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676275#M52370</guid>
      <dc:creator>JasonTipton</dc:creator>
      <dc:date>2021-12-12T04:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: Fault Taulerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676276#M52371</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you tried to capture the error explicitly?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import_arcpy = False&lt;/P&gt;&lt;P&gt;while import_arcpy == False:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import_arcpy = True&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for error in e:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if error == 'No module named arcpy':&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Edited: Oh I see in your code that you set arcpy_imported = True first. That way it would never enter your while loop? I think setting it to arcpy_imported = False first would fix your code as you have it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Nov 2015 18:10:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676276#M52371</guid>
      <dc:creator>LukeSturtevant</dc:creator>
      <dc:date>2015-11-16T18:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: Fault Taulerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676277#M52372</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There are some workarounds &lt;A href="http://gis.stackexchange.com/questions/91112/is-there-a-way-to-refresh-imported-modules-in-a-arcgis-python-toolbox" title="http://gis.stackexchange.com/questions/91112/is-there-a-way-to-refresh-imported-modules-in-a-arcgis-python-toolbox"&gt;http://gis.stackexchange.com/questions/91112/is-there-a-way-to-refresh-imported-modules-in-a-arcgis-python-toolbox&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://gis.stackexchange.com/questions/91112/is-there-a-way-to-refresh-imported-modules-in-a-arcgis-python-toolbox" title="http://gis.stackexchange.com/questions/91112/is-there-a-way-to-refresh-imported-modules-in-a-arcgis-python-toolbox"&gt;Is there a way to refresh imported modules in a ArcGIS Python Toolbox? - Geographic Information Systems Stack Exchange&lt;/A&gt; &lt;/P&gt;&lt;P&gt;but basically, it boils down to the reload &lt;A href="https://docs.python.org/2/library/functions.html#reload" title="https://docs.python.org/2/library/functions.html#reload"&gt;2. Built-in Functions — Python 2.7.10 documentation&lt;/A&gt; &lt;/P&gt;&lt;P&gt;pick your version (reload moved to imp) &lt;A href="https://docs.python.org/3/library/imp.html" title="https://docs.python.org/3/library/imp.html"&gt;36.2. imp — Access the import internals — Python 3.5.0 documentation&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Nov 2015 18:15:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676277#M52372</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2015-11-16T18:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Fault Taulerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676278#M52373</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry, that was supposed to be False. Edited my post to reflect.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And it doesn't seem to matter about the Exception. The Exception is: "RuntimeError: NotInitialized". But once it hits the exception once inside the session, you will never be able to import arcpy without starting a new session.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Added full traceback to my post.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Nov 2015 19:59:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676278#M52373</guid>
      <dc:creator>JasonTipton</dc:creator>
      <dc:date>2015-11-16T19:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: Fault Taulerant Python Services -- arcpy</title>
      <link>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676279#M52374</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yeah, I went down that route already. The problem is that it fails on import so "arcpy" doesn't exist so there's nothing to reload.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_1447704503936666" data-renderedposition="50_8_912_16" jivemacro_uid="_1447704503936666"&gt;&lt;P&gt;reload(arcpy)&lt;/P&gt;&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;/P&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;&amp;nbsp; File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P dir="ltr" style="padding-left: 30px;"&gt;&lt;SPAN style="color: #e23d39;"&gt;NameError: name 'arcpy' is not defined&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It seems to be something cached at a non-python level.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Nov 2015 20:12:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/fault-tolerant-python-services-arcpy/m-p/676279#M52374</guid>
      <dc:creator>JasonTipton</dc:creator>
      <dc:date>2015-11-16T20:12:46Z</dc:date>
    </item>
  </channel>
</rss>

