ignoring SIGPIPE in a python script?

D

Dan Stromberg

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.

Thanks!
 
D

Donn Cave

Dan Stromberg said:
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, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top