ignoring SIGPIPE in a python script?

Donn Cave donn at u.washington.edu
Wed Jun 1 12:35:12 EDT 2005


In article <pan.2005.06.01.01.26.56.994483 at dcs.nac.uci.edu>,
 Dan Stromberg <strombrg at dcs.nac.uci.edu> wrote:

> I have a python script that sometimes gets a SIGPIPE signal, and errors
> out.  And I want it to just terminate as though it had hit EOF.
> 
> I'm running:
> 
>    signal.signal(signal.SIGPIPE,signal.SIG_IGN)
> 
> ...in the main function, but the script is still erroring out on sigpipe
> when the consumer to its producer terminates early.
> 
> Am I going to have to code in a handful of exceptions, and then
> conditionalize what happens in those exception handlers, to get the
> desired behavior?
> 
> ISTR hearing that although bash notifies one of SIGPIPE errors, tcsh
> generally silently ignores them.  From this, I conclude that it might be
> reasonable for my script to ignore SIGPIPE.

This is a little idiosyncracy of Python's.  You can restore
the default signal handler,
   signal.signal(signal.SIGPIPE, signal.SIG_DFL)

and it will behave like a normal UNIX application.  These other
applications aren't precisely ignoring SIGPIPE, though.  If you
think about what that would look like, I guess it would mean they
would continue to write output, since it's truly a rare application
that checks its output (especially since it's usually buffered.)
The SIGPIPE signal just aborts the program.  Not really very
much like EOF at all.

You could install your own handler, but it would probably make
about as much sense to just trap the exception.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list