Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-socket.html" /> | |
13 | <link rel="prev" href="someos.html" /> | |
14 | <link rel="parent" href="someos.html" /> | |
15 | <link rel="next" href="node373.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>7.1 signal -- Set handlers for asynchronous events</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="7. Optional Operating System" | |
25 | href="someos.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="7. Optional Operating System" | |
28 | href="someos.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="7.1.1 Example" | |
31 | href="node373.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="someos.html">7. Optional Operating System</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node373.html">7.1.1 Example</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION009100000000000000000"> | |
56 | 7.1 <tt class="module">signal</tt> -- | |
57 | Set handlers for asynchronous events</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-signal"></A> | |
62 | ||
63 | <P> | |
64 | This module provides mechanisms to use signal handlers in Python. | |
65 | Some general rules for working with signals and their handlers: | |
66 | ||
67 | <P> | |
68 | ||
69 | <UL> | |
70 | <LI>A handler for a particular signal, once set, remains installed until | |
71 | it is explicitly reset (Python emulates the BSD style interface | |
72 | regardless of the underlying implementation), with the exception of | |
73 | the handler for <tt class="constant">SIGCHLD</tt>, which follows the underlying | |
74 | implementation. | |
75 | ||
76 | <P> | |
77 | </LI> | |
78 | <LI>There is no way to ``block'' signals temporarily from critical | |
79 | sections (since this is not supported by all <span class="Unix">Unix</span> flavors). | |
80 | ||
81 | <P> | |
82 | </LI> | |
83 | <LI>Although Python signal handlers are called asynchronously as far as | |
84 | the Python user is concerned, they can only occur between the | |
85 | ``atomic'' instructions of the Python interpreter. This means that | |
86 | signals arriving during long calculations implemented purely in C | |
87 | (such as regular expression matches on large bodies of text) may be | |
88 | delayed for an arbitrary amount of time. | |
89 | ||
90 | <P> | |
91 | </LI> | |
92 | <LI>When a signal arrives during an I/O operation, it is possible that the | |
93 | I/O operation raises an exception after the signal handler returns. | |
94 | This is dependent on the underlying <span class="Unix">Unix</span> system's semantics regarding | |
95 | interrupted system calls. | |
96 | ||
97 | <P> | |
98 | </LI> | |
99 | <LI>Because the C signal handler always returns, it makes little sense to | |
100 | catch synchronous errors like <tt class="constant">SIGFPE</tt> or <tt class="constant">SIGSEGV</tt>. | |
101 | ||
102 | <P> | |
103 | </LI> | |
104 | <LI>Python installs a small number of signal handlers by default: | |
105 | <tt class="constant">SIGPIPE</tt> is ignored (so write errors on pipes and sockets can be | |
106 | reported as ordinary Python exceptions) and <tt class="constant">SIGINT</tt> is translated | |
107 | into a <tt class="exception">KeyboardInterrupt</tt> exception. All of these can be | |
108 | overridden. | |
109 | ||
110 | <P> | |
111 | </LI> | |
112 | <LI>Some care must be taken if both signals and threads are used in the | |
113 | same program. The fundamental thing to remember in using signals and | |
114 | threads simultaneously is: always perform <tt class="function">signal()</tt> operations | |
115 | in the main thread of execution. Any thread can perform an | |
116 | <tt class="function">alarm()</tt>, <tt class="function">getsignal()</tt>, or <tt class="function">pause()</tt>; | |
117 | only the main thread can set a new signal handler, and the main thread | |
118 | will be the only one to receive signals (this is enforced by the | |
119 | Python <tt class="module">signal</tt> module, even if the underlying thread | |
120 | implementation supports sending signals to individual threads). This | |
121 | means that signals can't be used as a means of inter-thread | |
122 | communication. Use locks instead. | |
123 | ||
124 | <P> | |
125 | </LI> | |
126 | </UL> | |
127 | ||
128 | <P> | |
129 | The variables defined in the <tt class="module">signal</tt> module are: | |
130 | ||
131 | <P> | |
132 | <dl><dt><b><tt id='l2h-2588' xml:id='l2h-2588'>SIG_DFL</tt></b></dt> | |
133 | <dd> | |
134 | This is one of two standard signal handling options; it will simply | |
135 | perform the default function for the signal. For example, on most | |
136 | systems the default action for <tt class="constant">SIGQUIT</tt> is to dump core | |
137 | and exit, while the default action for <tt class="constant">SIGCLD</tt> is to | |
138 | simply ignore it. | |
139 | </dd></dl> | |
140 | ||
141 | <P> | |
142 | <dl><dt><b><tt id='l2h-2589' xml:id='l2h-2589'>SIG_IGN</tt></b></dt> | |
143 | <dd> | |
144 | This is another standard signal handler, which will simply ignore | |
145 | the given signal. | |
146 | </dd></dl> | |
147 | ||
148 | <P> | |
149 | <dl><dt><b><tt id='l2h-2590' xml:id='l2h-2590'>SIG*</tt></b></dt> | |
150 | <dd> | |
151 | All the signal numbers are defined symbolically. For example, the | |
152 | hangup signal is defined as <tt class="constant">signal.SIGHUP</tt>; the variable names | |
153 | are identical to the names used in C programs, as found in | |
154 | <code><signal.h></code>. | |
155 | The <span class="Unix">Unix</span> man page for `<tt class="cfunction">signal()</tt>' lists the existing | |
156 | signals (on some systems this is <span class="manpage"><i>signal</i>(2)</span>, on others the | |
157 | list is in <span class="manpage"><i>signal</i>(7)</span>). | |
158 | Note that not all systems define the same set of signal names; only | |
159 | those names defined by the system are defined by this module. | |
160 | </dd></dl> | |
161 | ||
162 | <P> | |
163 | <dl><dt><b><tt id='l2h-2591' xml:id='l2h-2591'>NSIG</tt></b></dt> | |
164 | <dd> | |
165 | One more than the number of the highest signal number. | |
166 | </dd></dl> | |
167 | ||
168 | <P> | |
169 | The <tt class="module">signal</tt> module defines the following functions: | |
170 | ||
171 | <P> | |
172 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
173 | <td><nobr><b><tt id='l2h-2592' xml:id='l2h-2592' class="function">alarm</tt></b>(</nobr></td> | |
174 | <td><var>time</var>)</td></tr></table></dt> | |
175 | <dd> | |
176 | If <var>time</var> is non-zero, this function requests that a | |
177 | <tt class="constant">SIGALRM</tt> signal be sent to the process in <var>time</var> seconds. | |
178 | Any previously scheduled alarm is canceled (only one alarm can | |
179 | be scheduled at any time). The returned value is then the number of | |
180 | seconds before any previously set alarm was to have been delivered. | |
181 | If <var>time</var> is zero, no alarm id scheduled, and any scheduled | |
182 | alarm is canceled. The return value is the number of seconds | |
183 | remaining before a previously scheduled alarm. If the return value | |
184 | is zero, no alarm is currently scheduled. (See the <span class="Unix">Unix</span> man page | |
185 | <span class="manpage"><i>alarm</i>(2)</span>.) | |
186 | Availability: <span class="Unix">Unix</span>. | |
187 | </dl> | |
188 | ||
189 | <P> | |
190 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
191 | <td><nobr><b><tt id='l2h-2593' xml:id='l2h-2593' class="function">getsignal</tt></b>(</nobr></td> | |
192 | <td><var>signalnum</var>)</td></tr></table></dt> | |
193 | <dd> | |
194 | Return the current signal handler for the signal <var>signalnum</var>. | |
195 | The returned value may be a callable Python object, or one of the | |
196 | special values <tt class="constant">signal.SIG_IGN</tt>, <tt class="constant">signal.SIG_DFL</tt> or | |
197 | <tt class="constant">None</tt>. Here, <tt class="constant">signal.SIG_IGN</tt> means that the | |
198 | signal was previously ignored, <tt class="constant">signal.SIG_DFL</tt> means that the | |
199 | default way of handling the signal was previously in use, and | |
200 | <code>None</code> means that the previous signal handler was not installed | |
201 | from Python. | |
202 | </dl> | |
203 | ||
204 | <P> | |
205 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
206 | <td><nobr><b><tt id='l2h-2594' xml:id='l2h-2594' class="function">pause</tt></b>(</nobr></td> | |
207 | <td><var></var>)</td></tr></table></dt> | |
208 | <dd> | |
209 | Cause the process to sleep until a signal is received; the | |
210 | appropriate handler will then be called. Returns nothing. Not on | |
211 | Windows. (See the <span class="Unix">Unix</span> man page <span class="manpage"><i>signal</i>(2)</span>.) | |
212 | </dl> | |
213 | ||
214 | <P> | |
215 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
216 | <td><nobr><b><tt id='l2h-2595' xml:id='l2h-2595' class="function">signal</tt></b>(</nobr></td> | |
217 | <td><var>signalnum, handler</var>)</td></tr></table></dt> | |
218 | <dd> | |
219 | Set the handler for signal <var>signalnum</var> to the function | |
220 | <var>handler</var>. <var>handler</var> can be a callable Python object | |
221 | taking two arguments (see below), or | |
222 | one of the special values <tt class="constant">signal.SIG_IGN</tt> or | |
223 | <tt class="constant">signal.SIG_DFL</tt>. The previous signal handler will be returned | |
224 | (see the description of <tt class="function">getsignal()</tt> above). (See the | |
225 | <span class="Unix">Unix</span> man page <span class="manpage"><i>signal</i>(2)</span>.) | |
226 | ||
227 | <P> | |
228 | When threads are enabled, this function can only be called from the | |
229 | main thread; attempting to call it from other threads will cause a | |
230 | <tt class="exception">ValueError</tt> exception to be raised. | |
231 | ||
232 | <P> | |
233 | The <var>handler</var> is called with two arguments: the signal number | |
234 | and the current stack frame (<code>None</code> or a frame object; | |
235 | for a description of frame objects, see the reference manual section | |
236 | on the standard type hierarchy or see the attribute descriptions in | |
237 | the <tt class="module"><a href="module-inspect.html">inspect</a></tt> module). | |
238 | </dl> | |
239 | ||
240 | <P> | |
241 | ||
242 | <p><br /></p><hr class='online-navigation' /> | |
243 | <div class='online-navigation'> | |
244 | <!--Table of Child-Links--> | |
245 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
246 | ||
247 | <UL CLASS="ChildLinks"> | |
248 | <LI><A href="node373.html">7.1.1 Example</a> | |
249 | </ul> | |
250 | <!--End of Table of Child-Links--> | |
251 | </div> | |
252 | ||
253 | <DIV CLASS="navigation"> | |
254 | <div class='online-navigation'> | |
255 | <p></p><hr /> | |
256 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
257 | <tr> | |
258 | <td class='online-navigation'><a rel="prev" title="7. Optional Operating System" | |
259 | href="someos.html"><img src='../icons/previous.png' | |
260 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
261 | <td class='online-navigation'><a rel="parent" title="7. Optional Operating System" | |
262 | href="someos.html"><img src='../icons/up.png' | |
263 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
264 | <td class='online-navigation'><a rel="next" title="7.1.1 Example" | |
265 | href="node373.html"><img src='../icons/next.png' | |
266 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
267 | <td align="center" width="100%">Python Library Reference</td> | |
268 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
269 | href="contents.html"><img src='../icons/contents.png' | |
270 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
271 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
272 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
273 | <td class='online-navigation'><a rel="index" title="Index" | |
274 | href="genindex.html"><img src='../icons/index.png' | |
275 | border='0' height='32' alt='Index' width='32' /></A></td> | |
276 | </tr></table> | |
277 | <div class='online-navigation'> | |
278 | <b class="navlabel">Previous:</b> | |
279 | <a class="sectref" rel="prev" href="someos.html">7. Optional Operating System</A> | |
280 | <b class="navlabel">Up:</b> | |
281 | <a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A> | |
282 | <b class="navlabel">Next:</b> | |
283 | <a class="sectref" rel="next" href="node373.html">7.1.1 Example</A> | |
284 | </div> | |
285 | </div> | |
286 | <hr /> | |
287 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
288 | </DIV> | |
289 | <!--End of Navigation Panel--> | |
290 | <ADDRESS> | |
291 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
292 | </ADDRESS> | |
293 | </BODY> | |
294 | </HTML> |