Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Signal Handling - should I even try in Python?

25 views
Skip to first unread message

Aaron Watters

unread,
Jul 8, 1996, 3:00:00 AM7/8/96
to

Frank Kim wrote:
>
> Below is a simple program that demonstrates how I've tried to do signal
> handling in Python.

Well, after I fixed your syntax error to make the program work I couldn't
get it to show any problems. (on slowlarus... oops... solaris).

Guido is on vacation, but I believe his official advice is: avoid
using signals if possible. signals interact weirdly sometimes with
built in operations: eg, blocking network reads suppress signals
sometimes, because they are in the middle of initializing a data
structure that must be completed before the python main loop can
resume in a consistent state -- you can get around this by doing
horrible things like reading one character at a time, which is okay
unless you're reading megabytes...

That's all I can say, unless you can come up with a scenario
that clearly demonstrates your problem. maybe someone else knows...
-- Aaron Watters
===
A stitch in time breeds contempt.

Frank Kim

unread,
Jul 9, 1996, 3:00:00 AM7/9/96
to

Below is a simple program that demonstrates how I've tried to do signal

handling in Python. As you can see I had to catch the IOError exception
so I exit gracefully. When I extended this idea to larger programs I
found myself trying to catch many exceptions and sometimes the program
wouldn't even exit. Does anyone have suggestions on how to properly do
signal handling? Thanks!

#!/usr/local/bin/python

# July 2, 1996
# Test catching signals.

import signal, time, sys

exitFlag = 0

def handler (signum, frame):
global exitFlag
print "howdy"
exitFlag = 1

def mysleep(secs): # could get even fancier...
done = 0
while not done:
try:
time.sleep(secs)
done = 1
except IOError:
pass


signal.signal (signal.SIGABRT, handler )
signal.signal (signal.SIGALRM, handler )
signal.signal (signal.SIGHUP, handler )
signal.signal (signal.SIGINT, handler )
signal.signal (signal.SIGQUIT, handler )
signal.signal (signal.SIGTERM, handler )
signal.signal (signal.SIGTSTP, handler )

while (1):
mysleep (1)
print "I'm sleeping."
if exitFlag == 1:
sys.exit()
--
Frank Kim fran...@catfish.lcs.mit.edu
http://cag-www.lcs.mit.edu/~frankkim/

0 new messages