(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-socket.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-signal.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/genindex.html| ]]
|
Next: 7.2 socket Up: 7.1 signal Previous: 7.1 signal
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/152/lib/module-socket.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-signal.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-signal.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/genindex.html| ]]
|
Next: 7.2 socket Up: 7.1 signal Previous: 7.1 signal
Send comments on this document to python-docs@python.org.