<?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: KeyboardInterrupt doesn't work when arcgisscripting module is loaded in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585165#M19344</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is a soft interrupt. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Click on the Pythonwin icon in the tray at the bottom of the screen and you will get an option "break into running code" that will do a soft interrupt, very useful if you have got into a loop by forgetting the row = cur.next().&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 27 Sep 2010 10:27:22 GMT</pubDate>
    <dc:creator>KimOllivier</dc:creator>
    <dc:date>2010-09-27T10:27:22Z</dc:date>
    <item>
      <title>KeyboardInterrupt doesn't work when arcgisscripting module is loaded</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585164#M19343</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The KeyboardInterrupt exception -- thrown when Ctrl-C, or Ctrl-Break on some machines, are pressed in a Python console window -- is commonly used to gracefully break out of long-process loops, allowing your script to perform cleanup, logging etc, before exiting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Under my ArcGIS 9.3.1 SP2/Windows XP SP3 environment, there is an issue with catching the KeyboardInterrupt exception if the arcgisscripting module is loaded. Instead of hitting the KeyboardInterrupt catch statement, the script aborts abruptly, spitting out the following message: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;forrtl: error (200): program aborting due to control-C event&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the arcgisscripting module is not loaded (removed from the import statement), the KeyboardInterrupt exception is thrown and processed properly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From what I've read this is due to the use of Fortran in the arcgisscripting module which somehow overrides Python's interrupt processing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's an example script you can use to test and reproduce the issue. Save it as a Python script, run it from the Windows command prompt and press Ctrl-C (Ctrl-Break on some machines) to test whether the KeyboardInterrupt exception is processed. Simply toggle the commenting for the "import arcgisscripting" line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#import arcgisscripting
import time

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; while(True):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(1)
except KeyboardInterrupt:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "KeyboardInterrupt detected!"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyone know of a workaround to this problem? Thanks in advance for any insights you may have.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Sep 2010 17:58:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585164#M19343</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2010-09-24T17:58:18Z</dc:date>
    </item>
    <item>
      <title>Re: KeyboardInterrupt doesn't work when arcgisscripting module is loaded</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585165#M19344</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is a soft interrupt. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Click on the Pythonwin icon in the tray at the bottom of the screen and you will get an option "break into running code" that will do a soft interrupt, very useful if you have got into a loop by forgetting the row = cur.next().&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Sep 2010 10:27:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585165#M19344</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2010-09-27T10:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: KeyboardInterrupt doesn't work when arcgisscripting module is loaded</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585166#M19345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yeah that is handy for debugging, not so much for production though. I received a response on GIS Stack Exchange that sounds promising, have not tested yet:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You can try reinstalling a custom handler for SIGINT using the &lt;A href="http://docs.python.org/release/2.5.4/lib/module-signal.html" rel="nofollow noopener noreferrer" target="_blank"&gt;signal module&lt;/A&gt; after importing arcgisscripting.&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcgisscripting
import signal

def ctrlc(sig, frame):
&amp;nbsp;&amp;nbsp;&amp;nbsp; raise KeyboardInterrupt("CTRL-C!")

signal.signal(signal.SIGINT, ctrlc)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;A href="http://gis.stackexchange.com/questions/2190/keyboardinterrupt-doesnt-work-when-arcgisscripting-module-is-loaded/2197#2197" rel="nofollow noopener noreferrer" target="_blank"&gt;http://gis.stackexchange.com/questions/2190/keyboardinterrupt-doesnt-work-when-arcgisscripting-module-is-loaded/2197#2197&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;edit: It unfortunately doesn't seem to work, the forrtl error still occurs instead of my KeyboardInterrupt exception. Also, the help file on the signal module says that SIGINT is already translated to KeyboardInterrupt by default. So unfortunately it looks like arcgisscripting is still overriding it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:09:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585166#M19345</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2021-12-12T01:09:32Z</dc:date>
    </item>
    <item>
      <title>Re: KeyboardInterrupt doesn't work when arcgisscripting module is loaded</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585167#M19346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I figured out a Windows-only workaround for this. It uses the &lt;/SPAN&gt;&lt;STRONG&gt;msvcrt&lt;/STRONG&gt;&lt;SPAN&gt; module, which is a Windows-only wrapper for the Visual C++ runtime, to read which keyboard character code was last received. It doesn't override the Ctrl-C, so I specified it should look for Ctrl-Alt-Q. When you press Ctrl-Alt-Q in my test script it will raise the KeyboardInterrupt exception. If you hit Ctrl-C it will still abruptly kill the script, however.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There are likely other ways to do this on *nix but I only needed this for Windows.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcgisscripting
import msvcrt
import time

def kbfunc():
&amp;nbsp;&amp;nbsp; x = msvcrt.kbhit()
&amp;nbsp;&amp;nbsp; if x:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = ord(msvcrt.getch())
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ret = 0
&amp;nbsp;&amp;nbsp; return ret

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(True):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; time.sleep(1)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check to see if Ctrl-Alt-Q is pressed
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = kbfunc()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x == 16: # Ctrl-Alt-Q
&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; raise KeyboardInterrupt

&amp;nbsp;&amp;nbsp;&amp;nbsp; except KeyboardInterrupt:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "KeyboardInterrupt detected!"
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:39:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/keyboardinterrupt-doesn-t-work-when/m-p/585167#M19346</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2021-12-12T16:39:44Z</dc:date>
    </item>
  </channel>
</rss>

