home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / s / signal < prev    next >
Encoding:
Text File  |  1996-11-14  |  6.5 KB  |  125 lines

  1. <TITLE>signal -- Python library reference</TITLE>
  2. Next: <A HREF="../s/socket" TYPE="Next">socket</A>  
  3. Prev: <A HREF="../o/optional_operating_system_services" TYPE="Prev">Optional Operating System Services</A>  
  4. Up: <A HREF="../o/optional_operating_system_services" TYPE="Up">Optional Operating System Services</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>7.1. Built-in Module <CODE>signal</CODE></H1>
  7. This module provides mechanisms to use signal handlers in Python.
  8. Some general rules for working with signals handlers:
  9. <P>
  10. <UL>
  11. <LI>•  A handler for a particular signal, once set, remains installed until
  12. it is explicitly reset (i.e. Python emulates the BSD style interface
  13. regardless of the underlying implementation), with the exception of
  14. the handler for <CODE>SIGCHLD</CODE>, which follows the underlying
  15. implementation.
  16. <P>
  17. <LI>•  There is no way to ``block'' signals temporarily from critical
  18. sections (since this is not supported by all UNIX flavors).
  19. <P>
  20. <LI>•  Although Python signal handlers are called asynchronously as far as
  21. the Python user is concerned, they can only occur between the
  22. ``atomic'' instructions of the Python interpreter.  This means that
  23. signals arriving during long calculations implemented purely in C
  24. (e.g. regular expression matches on large bodies of text) may be
  25. delayed for an arbitrary amount of time.
  26. <P>
  27. <LI>•  When a signal arrives during an I/O operation, it is possible that the
  28. I/O operation raises an exception after the signal handler returns.
  29. This is dependent on the underlying UNIX system's semantics regarding
  30. interrupted system calls.
  31. <P>
  32. <LI>•  Because the C signal handler always returns, it makes little sense to
  33. catch synchronous errors like <CODE>SIGFPE</CODE> or <CODE>SIGSEGV</CODE>.
  34. <P>
  35. <LI>•  Python installs a small number of signal handlers by default:
  36. <CODE>SIGPIPE</CODE> is ignored (so write errors on pipes and sockets can be
  37. reported as ordinary Python exceptions), <CODE>SIGINT</CODE> is translated
  38. into a <CODE>KeyboardInterrupt</CODE> exception, and <CODE>SIGTERM</CODE> is
  39. caught so that necessary cleanup (especially <CODE>sys.exitfunc</CODE>) can
  40. be performed before actually terminating.  All of these can be
  41. overridden.
  42. <P>
  43. <LI>•  Some care must be taken if both signals and threads are used in the
  44. same program.  The fundamental thing to remember in using signals and
  45. threads simultaneously is: always perform <CODE>signal()</CODE> operations
  46. in the main thread of execution.  Any thread can perform an
  47. <CODE>alarm()</CODE>, <CODE>getsignal()</CODE>, or <CODE>pause()</CODE>; only the main
  48. thread can set a new signal handler, and the main thread will be the
  49. only one to receive signals (this is enforced by the Python signal
  50. module, even if the underlying thread implementation supports sending
  51. signals to individual threads).  This means that signals can't be used
  52. as a means of interthread communication.  Use locks instead.
  53. <P>
  54. </UL>
  55. The variables defined in the signal module are:
  56. <P>
  57. <DL><DT><B>SIG_DFL</B> -- data of module signal<DD>
  58. This is one of two standard signal handling options; it will simply
  59. perform the default function for the signal.  For example, on most
  60. systems the default action for SIGQUIT is to dump core and exit,
  61. while the default action for SIGCLD is to simply ignore it.
  62. </DL>
  63. <DL><DT><B>SIG_IGN</B> -- data of module signal<DD>
  64. This is another standard signal handler, which will simply ignore
  65. the given signal.
  66. </DL>
  67. <DL><DT><B>SIG*</B> -- data of module signal<DD>
  68. All the signal numbers are defined symbolically.  For example, the
  69. hangup signal is defined as <CODE>signal.SIGHUP</CODE>; the variable names
  70. are identical to the names used in C programs, as found in
  71. <FILE>signal.h</FILE>.
  72. The UNIX man page for <FILE>signal</FILE> lists the existing signals (on
  73. some systems this is <FILE>signal(2)</FILE>, on others the list is in
  74. <FILE>signal(7)</FILE>).
  75. Note that not all systems define the same set of signal names; only
  76. those names defined by the system are defined by this module.
  77. </DL>
  78. <DL><DT><B>NSIG</B> -- data of module signal<DD>
  79. One more than the number of the highest signal number.
  80. </DL>
  81. The signal module defines the following functions:
  82. <P>
  83. <DL><DT><B>alarm</B> (<VAR>time</VAR>) -- function of module signal<DD>
  84. If <VAR>time</VAR> is non-zero, this function requests that a
  85. <CODE>SIGALRM</CODE> signal be sent to the process in <VAR>time</VAR> seconds.
  86. Any previously scheduled alarm is canceled (i.e. only one alarm can
  87. be scheduled at any time).  The returned value is then the number of
  88. seconds before any previously set alarm was to have been delivered.
  89. If <VAR>time</VAR> is zero, no alarm id scheduled, and any scheduled
  90. alarm is canceled.  The return value is the number of seconds
  91. remaining before a previously scheduled alarm.  If the return value
  92. is zero, no alarm is currently scheduled.  (See the UNIX man page
  93. <CODE>alarm(2)</CODE>.)
  94. </DL>
  95. <DL><DT><B>getsignal</B> (<VAR>signalnum</VAR>) -- function of module signal<DD>
  96. Return the current signal handler for the signal <VAR>signalnum</VAR>.
  97. The returned value may be a callable Python object, or one of the
  98. special values <CODE>signal.SIG_IGN</CODE>, <CODE>signal.SIG_DFL</CODE> or
  99. <CODE>None</CODE>.  Here, <CODE>signal.SIG_IGN</CODE> means that the signal was
  100. previously ignored, <CODE>signal.SIG_DFL</CODE> means that the default way
  101. of handling the signal was previously in use, and <CODE>None</CODE> means
  102. that the previous signal handler was not installed from Python.
  103. </DL>
  104. <DL><DT><B>pause</B> () -- function of module signal<DD>
  105. Cause the process to sleep until a signal is received; the
  106. appropriate handler will then be called.  Returns nothing.  (See the
  107. UNIX man page <CODE>signal(2)</CODE>.)
  108. </DL>
  109. <DL><DT><B>signal</B> (<VAR>signalnum</VAR>, <VAR>handler</VAR>) -- function of module signal<DD>
  110. Set the handler for signal <VAR>signalnum</VAR> to the function
  111. <VAR>handler</VAR>.  <VAR>handler</VAR> can be any callable Python object, or
  112. one of the special values <CODE>signal.SIG_IGN</CODE> or
  113. <CODE>signal.SIG_DFL</CODE>.  The previous signal handler will be returned
  114. (see the description of <CODE>getsignal()</CODE> above).  (See the UNIX
  115. man page <CODE>signal(2)</CODE>.)
  116. <P>
  117. When threads are enabled, this function can only be called from the
  118. main thread; attempting to call it from other threads will cause a
  119. <CODE>ValueError</CODE> exception to be raised.
  120. <P>
  121. The <VAR>handler</VAR> is called with two arguments: the signal number
  122. and the current stack frame (<CODE>None</CODE> or a frame object; see the
  123. reference manual for a description of frame objects).
  124. </DL>
  125.