473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Signals and keyboard interupts

[Using Windows XP and Python 2.4.1]

I have question about the following code, which basically accomplished
what what I want, which is to gracefully exit the main loop when the
user presses either the control-c or control-break key:

import signal
import sys
import time
import traceback

QUIT = False

def mySigHandler(*whatever):
global QUIT # Change value of QUIT
QUIT = True
print
print "Interrupt caught and QUIT flag set"

# call the above procedure, when control-c or control-break is pressed.
old_SIGINT_Handler = signal.signal(signal.SIGINT, mySigHandler)
old_SIGBREAK_Handler = signal.signal(signal.SIGBREAK, mySigHandler)

while 1:
try:
if QUIT:
break # exit loop

print "Processing..."
time.sleep(2)

except IOError, (errno, strerror):
if errno == 4: # Interrupted function call
# mySigHandler called, so will ignore here
continue
else: # some other IOerror -- print info and break out of loop
print "IOError[%s] exception occured: %s" % (errno,
strerror)
traceback.print_exc(file=sys.stdout)
break

print "finished"
# restore old signal handlers
signal.signal(signal.SIGINT, old_SIGINT_Handler)
signal.signal(signal.SIGBREAK, old_SIGBREAK_Handler)

My question is why is the a try/except block necessary? If it's left
out, and unhandled exception occurs. Seems like catching the signals
before python's default handler gets them should prevent it from being
turned into an exception. I don't understand how this is happening in
the above code.

I read several other posts about the subject, notably the one by Bengt
Richter in
http://groups.google.com/group/comp....f08170a?hl=en&,
but haven't been able to figure out why the [much more involved]
example in his post does not seem to exhibit this problem (i.e. it has
no try/except block).

Thanks in advance for any help.
-Martin

Oct 27 '05 #1
0 1412

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Holger Joukl | last post by:
Hi, migrating from good old python 1.5.2 to python 2.3, I have a problem running a program that features some threads which execute calls to an extension module. Problem is that all of a sudden,...
1
by: Isidro Vila Verde | last post by:
Greetings, I need to handle signals to close some excel applications that my script open, when the script is killed by another process. My script is a little bit complex, but I wrote just two ...
4
by: Gabriele Bartolini | last post by:
Hi, I am writing an application in C++ on Linux, using threads (this is my first experience with pthreads). The application itself is fine, it is just that I wanted to handle asynchronous...
1
by: Leo Kirch | last post by:
Hello XSLT gurus, i've got a rather difficult problem. Some explanations first. theres a signal oriented xml-file - the graphical represantation looks like: | startsignal (signal00) \...
4
by: maxmagna | last post by:
Hello, I am looking for material that describes how *precisely* UNIX-based signals interact with the normal C++ execution flow of control and guidelines on how to treat signals in an...
0
by: Arnaud Debaene | last post by:
Hello all. I've got a bunch of existing, non managed, C++ DLLs that export types with, among other things, public events implemented using the boost::signals library. Now, I need to have...
2
by: roaddogg | last post by:
Greetings, I am stuck on a topic for TAFE and cannot get my head around it. I need to Write a program to use a D to A converter board to output various signals which can be viewed on an...
11
by: vippstar | last post by:
What is the purpose of signals and why do they exist in C? thanks in advance
2
by: wongjoekmeu | last post by:
Dear All, I have some a program in which I link a static library. The static library has a initialize() and uninitialized() function. Now when I call the initialize function a thread is being...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.