(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-socket.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/genindex.html| ]]
|
Previous: 7.1 signal Up: 7.1 signal Next: 7.2 socket
7.1.1 Example
Here is a minimal example program. It uses the alarm() function to limit the time spent waiting to open a file; this is useful if the file is for a serial device that may not be turned on, which would normally cause the os.open() to hang indefinitely. The solution is to set a 5-second alarm before opening the file; if the operation takes too long, the alarm signal will be sent, and the handler raises an exception.
-
import signal, os, FCNTL
def handler(signum, frame):
print 'Signal handler called with signal', signum
raise IOError, "Couldn't open device!"
# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)
# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', FCNTL.O_RDWR)
signal.alarm(0) # Disable the alarm
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/module-socket.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/20/lib/genindex.html| ]]
|
Previous: 7.1 signal Up: 7.1 signal Next: 7.2 socket
See About this document... for information on suggesting changes.