<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7.1 signal --
Set handlers for asynchronous events.</TITLE>
<META NAME="description" CONTENT="7.1 signal --
Set handlers for asynchronous events.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-socket.html">
<LINK REL="previous" HREF="someos.html">
<LINK REL="up" HREF="someos.html">
<LINK REL="next" HREF="Signal_Example.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="Signal_Example.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="Signal_Example.html">7.1.1 Example</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009100000000000000000">
7.1 <tt class=module>signal</tt> --
         Set handlers for asynchronous events.</A>
</H1>

<P>

<P>
This module provides mechanisms to use signal handlers in Python.
Some general rules for working with signals and their handlers:

<P>

<UL><LI>A handler for a particular signal, once set, remains installed until
it is explicitly reset (i.e. Python emulates the BSD style interface
regardless of the underlying implementation), with the exception of
the handler for <tt class=constant>SIGCHLD</tt>, which follows the underlying
implementation.

<P>

<LI>There is no way to ``block'' signals temporarily from critical
sections (since this is not supported by all Unix flavors).

<P>

<LI>Although Python signal handlers are called asynchronously as far as
the Python user is concerned, they can only occur between the
``atomic'' instructions of the Python interpreter.  This means that
signals arriving during long calculations implemented purely in C
(e.g. regular expression matches on large bodies of text) may be
delayed for an arbitrary amount of time.

<P>

<LI>When a signal arrives during an I/O operation, it is possible that the
I/O operation raises an exception after the signal handler returns.
This is dependent on the underlying Unix system's semantics regarding
interrupted system calls.

<P>

<LI>Because the C signal handler always returns, it makes little sense to
catch synchronous errors like <tt class=constant>SIGFPE</tt> or <tt class=constant>SIGSEGV</tt>.

<P>

<LI>Python installs a small number of signal handlers by default:
<tt class=constant>SIGPIPE</tt> is ignored (so write errors on pipes and sockets can be
reported as ordinary Python exceptions) and <tt class=constant>SIGINT</tt> is translated
into a <tt class=exception>KeyboardInterrupt</tt> exception.  All of these can be
overridden.

<P>

<LI>Some care must be taken if both signals and threads are used in the
same program.  The fundamental thing to remember in using signals and
threads simultaneously is: always perform <tt class=function>signal()</tt> operations
in the main thread of execution.  Any thread can perform an
<tt class=function>alarm()</tt>, <tt class=function>getsignal()</tt>, or <tt class=function>pause()</tt>;
only the main thread can set a new signal handler, and the main thread
will be the only one to receive signals (this is enforced by the
Python <tt class=module>signal</tt> module, even if the underlying thread
implementation supports sending signals to individual threads).  This
means that signals can't be used as a means of interthread
communication.  Use locks instead.

<P>

</UL>

<P>
The variables defined in the <tt class=module>signal</tt> module are:

<P>
<dl><dt><b><a name="l2h-1249"><tt>SIG_DFL</tt></a></b>
<dd>
  This is one of two standard signal handling options; it will simply
  perform the default function for the signal.  For example, on most
  systems the default action for <tt class=constant>SIGQUIT</tt> is to dump core
  and exit, while the default action for <tt class=constant>SIGCLD</tt> is to
  simply ignore it.
</dl>

<P>
<dl><dt><b><a name="l2h-1250"><tt>SIG_IGN</tt></a></b>
<dd>
  This is another standard signal handler, which will simply ignore
  the given signal.
</dl>

<P>
<dl><dt><b><a name="l2h-1251"><tt>SIG*</tt></a></b>
<dd>
  All the signal numbers are defined symbolically.  For example, the
  hangup signal is defined as <tt class=constant>signal.SIGHUP</tt>; the variable names
  are identical to the names used in C programs, as found in
  <tt>&lt;signal.h&gt;</tt>.
  The Unix man page for `<tt class=cfunction>signal()</tt>' lists the existing
  signals (on some systems this is <span class=manpage><i>signal</i>(2)</span>, on others the
  list is in <span class=manpage><i>signal</i>(7)</span>).
  Note that not all systems define the same set of signal names; only
  those names defined by the system are defined by this module.
</dl>

<P>
<dl><dt><b><a name="l2h-1252"><tt>NSIG</tt></a></b>
<dd>
  One more than the number of the highest signal number.
</dl>

<P>
The <tt class=module>signal</tt> module defines the following functions:

<P>
<dl><dt><b><a name="l2h-1253"><tt class=function>alarm</tt></a></b> (<var>time</var>)
<dd>
  If <var>time</var> is non-zero, this function requests that a
  <tt class=constant>SIGALRM</tt> signal be sent to the process in <var>time</var> seconds.
  Any previously scheduled alarm is canceled (i.e. only one alarm can
  be scheduled at any time).  The returned value is then the number of
  seconds before any previously set alarm was to have been delivered.
  If <var>time</var> is zero, no alarm id scheduled, and any scheduled
  alarm is canceled.  The return value is the number of seconds
  remaining before a previously scheduled alarm.  If the return value
  is zero, no alarm is currently scheduled.  (See the Unix man page
  <span class=manpage><i>alarm</i>(2)</span>.)
</dl>

<P>
<dl><dt><b><a name="l2h-1254"><tt class=function>getsignal</tt></a></b> (<var>signalnum</var>)
<dd>
  Return the current signal handler for the signal <var>signalnum</var>.
  The returned value may be a callable Python object, or one of the
  special values <tt class=constant>signal.SIG_IGN</tt>, <tt class=constant>signal.SIG_DFL</tt> or
  <tt class=constant>None</tt>.  Here, <tt class=constant>signal.SIG_IGN</tt> means that the
  signal was previously ignored, <tt class=constant>signal.SIG_DFL</tt> means that the
  default way of handling the signal was previously in use, and
  <tt>None</tt> means that the previous signal handler was not installed
  from Python.
</dl>

<P>
<dl><dt><b><a name="l2h-1255"><tt class=function>pause</tt></a></b> ()
<dd>
  Cause the process to sleep until a signal is received; the
  appropriate handler will then be called.  Returns nothing.  (See the
  Unix man page <span class=manpage><i>signal</i>(2)</span>.)
</dl>

<P>
<dl><dt><b><a name="l2h-1256"><tt class=function>signal</tt></a></b> (<var>signalnum, handler</var>)
<dd>
  Set the handler for signal <var>signalnum</var> to the function
  <var>handler</var>.  <var>handler</var> can be a callable Python object
  taking two arguments (see below), or
  one of the special values <tt class=constant>signal.SIG_IGN</tt> or
  <tt class=constant>signal.SIG_DFL</tt>.  The previous signal handler will be returned
  (see the description of <tt class=function>getsignal()</tt> above).  (See the
  Unix man page <span class=manpage><i>signal</i>(2)</span>.)

<P>
When threads are enabled, this function can only be called from the
  main thread; attempting to call it from other threads will cause a
  <tt class=exception>ValueError</tt> exception to be raised.

<P>
The <var>handler</var> is called with two arguments: the signal number
  and the current stack frame (<tt>None</tt> or a frame object; see the
  reference manual for a description of frame objects).
<a name="l2h-1257"></a>
</dl>

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html2103"
 HREF="Signal_Example.html">7.1.1 Example</A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="Signal_Example.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="Signal_Example.html">7.1.1 Example</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
