rest25/library/signal.rst => rest262/library/signal.rst
34* Python installs a small number of signal handlers by default: :const:`SIGPIPE`
35  is ignored (so write errors on pipes and sockets can be reported as ordinary
36  Python exceptions) and :const:`SIGINT` is translated into a
37  :exc:`KeyboardInterrupt` exception.  All of these can be overridden.
38
39* Some care must be taken if both signals and threads are used in the same
40  program.  The fundamental thing to remember in using signals and threads
41  simultaneously is: always perform :func:`signal` operations in the main thread
n42-  of execution.  Any thread can perform an :func:`alarm`, :func:`getsignal`, or
n42+  of execution.  Any thread can perform an :func:`alarm`, :func:`getsignal`,
43-  :func:`pause`; only the main thread can set a new signal handler, and the main
43+  :func:`pause`, :func:`setitimer` or :func:`getitimer`; only the main thread
44-  thread will be the only one to receive signals (this is enforced by the Python
44+  can set a new signal handler, and the main thread will be the only one to
45-  :mod:`signal` module, even if the underlying thread implementation supports
45+  receive signals (this is enforced by the Python :mod:`signal` module, even
46+  if the underlying thread implementation supports sending signals to
46-  sending signals to individual threads).  This means that signals can't be used
47+  individual threads).  This means that signals can't be used as a means of
47-  as a means of inter-thread communication.  Use locks instead.
48+  inter-thread communication.  Use locks instead.
48
49The variables defined in the :mod:`signal` module are:
50
51
52.. data:: SIG_DFL
53
n54-   This is one of two standard signal handling options; it will simply perform the
n55+   This is one of two standard signal handling options; it will simply perform
55-   default function for the signal.  For example, on most systems the default
56+   the default function for the signal.  For example, on most systems the
56-   action for :const:`SIGQUIT` is to dump core and exit, while the default action
57+   default action for :const:`SIGQUIT` is to dump core and exit, while the
57-   for :const:`SIGCLD` is to simply ignore it.
58+   default action for :const:`SIGCHLD` is to simply ignore it.
58
59
60.. data:: SIG_IGN
61
62   This is another standard signal handler, which will simply ignore the given
63   signal.
64
65
73   not all systems define the same set of signal names; only those names defined by
74   the system are defined by this module.
75
76
77.. data:: NSIG
78
79   One more than the number of the highest signal number.
80
n82+ 
83+.. data:: ITIMER_REAL
84+ 
85+   Decrements interval timer in real time, and delivers :const:`SIGALRM` upon expiration.
86+ 
87+ 
88+.. data:: ITIMER_VIRTUAL
89+ 
90+   Decrements interval timer only when the process is executing, and delivers
91+   SIGVTALRM upon expiration.
92+ 
93+ 
94+.. data:: ITIMER_PROF
95+ 
96+   Decrements interval timer both when the process executes and when the
97+   system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL,
98+   this timer is usually used to profile the time spent by the application
99+   in user and kernel space. SIGPROF is delivered upon expiration.
100+ 
101+ 
102+The :mod:`signal` module defines one exception:
103+ 
104+.. exception:: ItimerError
105+ 
106+   Raised to signal an error from the underlying :func:`setitimer` or
107+   :func:`getitimer` implementation. Expect this error if an invalid
108+   interval timer or a negative time is passed to :func:`setitimer`.
109+   This error is a subtype of :exc:`IOError`.
110+ 
111+ 
81The :mod:`signal` module defines the following functions:
82
83
84.. function:: alarm(time)
85
86   If *time* is non-zero, this function requests that a :const:`SIGALRM` signal be
87   sent to the process in *time* seconds. Any previously scheduled alarm is
88   canceled (only one alarm can be scheduled at any time).  The returned value is
89   then the number of seconds before any previously set alarm was to have been
90   delivered. If *time* is zero, no alarm is scheduled, and any scheduled alarm is
n91-   canceled.  The return value is the number of seconds remaining before a
n122+   canceled.  If the return value is zero, no alarm is currently scheduled.  (See
92-   previously scheduled alarm.  If the return value is zero, no alarm is currently
93-   scheduled.  (See the Unix man page :manpage:`alarm(2)`.) Availability: Unix.
123+   the Unix man page :manpage:`alarm(2)`.) Availability: Unix.
94
95
96.. function:: getsignal(signalnum)
97
98   Return the current signal handler for the signal *signalnum*. The returned value
99   may be a callable Python object, or one of the special values
100   :const:`signal.SIG_IGN`, :const:`signal.SIG_DFL` or :const:`None`.  Here,
101   :const:`signal.SIG_IGN` means that the signal was previously ignored,
106
107.. function:: pause()
108
109   Cause the process to sleep until a signal is received; the appropriate handler
110   will then be called.  Returns nothing.  Not on Windows. (See the Unix man page
111   :manpage:`signal(2)`.)
112
113
n144+.. function:: setitimer(which, seconds[, interval])
145+ 
146+   Sets given interval timer (one of :const:`signal.ITIMER_REAL`,
147+   :const:`signal.ITIMER_VIRTUAL` or :const:`signal.ITIMER_PROF`) specified
148+   by *which* to fire after *seconds* (float is accepted, different from
149+   :func:`alarm`) and after that every *interval* seconds. The interval
150+   timer specified by *which* can be cleared by setting seconds to zero.
151+ 
152+   When an interval timer fires, a signal is sent to the process.
153+   The signal sent is dependent on the timer being used;
154+   :const:`signal.ITIMER_REAL` will deliver :const:`SIGALRM`,
155+   :const:`signal.ITIMER_VIRTUAL` sends :const:`SIGVTALRM`,
156+   and :const:`signal.ITIMER_PROF` will deliver :const:`SIGPROF`.
157+ 
158+   The old values are returned as a tuple: (delay, interval).
159+ 
160+   Attempting to pass an invalid interval timer will cause a
161+   :exc:`ItimerError`.
162+ 
163+   .. versionadded:: 2.6
164+ 
165+ 
166+.. function:: getitimer(which)
167+ 
168+   Returns current value of a given interval timer specified by *which*.
169+ 
170+   .. versionadded:: 2.6
171+ 
172+ 
173+.. function:: set_wakeup_fd(fd)
174+ 
175+   Set the wakeup fd to *fd*.  When a signal is received, a ``'\0'`` byte is
176+   written to the fd.  This can be used by a library to wakeup a poll or select
177+   call, allowing the signal to be fully processed.
178+ 
179+   The old wakeup fd is returned.  *fd* must be non-blocking.  It is up to the
180+   library to remove any bytes before calling poll or select again.
181+ 
182+   When threads are enabled, this function can only be called from the main thread;
183+   attempting to call it from other threads will cause a :exc:`ValueError`
184+   exception to be raised.
185+ 
186+ 
187+.. function:: siginterrupt(signalnum, flag)
188+ 
189+   Change system call restart behaviour: if *flag* is :const:`False`, system calls
190+   will be restarted when interrupted by signal *signalnum*, otherwise system calls will
191+   be interrupted. Returns nothing. Availability: Unix (see the man page
192+   :manpage:`siginterrupt(3)` for further information).
193+ 
194+   Note that installing a signal handler with :func:`signal` will reset the restart
195+   behaviour to interruptible by implicitly calling :cfunc:`siginterrupt` with a true *flag*
196+   value for the given signal.
197+ 
198+   .. versionadded:: 2.6
199+ 
200+ 
114.. function:: signal(signalnum, handler)
115
116   Set the handler for signal *signalnum* to the function *handler*.  *handler* can
117   be a callable Python object taking two arguments (see below), or one of the
118   special values :const:`signal.SIG_IGN` or :const:`signal.SIG_DFL`.  The previous
119   signal handler will be returned (see the description of :func:`getsignal`
120   above).  (See the Unix man page :manpage:`signal(2)`.)
121
124   exception to be raised.
125
126   The *handler* is called with two arguments: the signal number and the current
127   stack frame (``None`` or a frame object; for a description of frame objects, see
128   the reference manual section on the standard type hierarchy or see the attribute
129   descriptions in the :mod:`inspect` module).
130
131
n219+.. _signal-example:
220+ 
132Example
133-------
n134- 
135-.. _signal example:
136
137Here is a minimal example program. It uses the :func:`alarm` function to limit
138the time spent waiting to open a file; this is useful if the file is for a
139serial device that may not be turned on, which would normally cause the
140:func:`os.open` to hang indefinitely.  The solution is to set a 5-second alarm
141before opening the file; if the operation takes too long, the alarm signal will
142be sent, and the handler raises an exception. ::
143
147       print 'Signal handler called with signal', signum
148       raise IOError, "Couldn't open device!"
149
150   # Set the signal handler and a 5-second alarm
151   signal.signal(signal.SIGALRM, handler)
152   signal.alarm(5)
153
154   # This open() may hang indefinitely
t155-   fd = os.open('/dev/ttyS0', os.O_RDWR)  
t242+   fd = os.open('/dev/ttyS0', os.O_RDWR)
156
157   signal.alarm(0)          # Disable the alarm
158
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op