Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a2510732
Commit
a2510732
authored
Aug 17, 2018
by
Alfred Perlstein
Committed by
Łukasz Langa
Aug 17, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Warn not to set SIGPIPE to SIG_DFL (#6773)
parent
0e6e7a1e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
0 deletions
+34
-0
Doc/library/signal.rst
Doc/library/signal.rst
+34
-0
No files found.
Doc/library/signal.rst
View file @
a2510732
...
...
@@ -503,3 +503,37 @@ be sent, and the handler raises an exception. ::
signal.alarm(0) # Disable the alarm
Note on SIGPIPE
---------------
Piping output of your program to tools like :manpage:`head(1)` will
cause a :const:`SIGPIPE` signal to be sent to your process when the receiver
of its standard output closes early. This results in an exception
like :code:`BrokenPipeError: [Errno 32] Broken pipe`. To handle this
case, wrap your entry point to catch this exception as follows::
import os
import sys
def main():
try:
# simulate large output (your code replaces this loop)
for x in range(10000):
print("y")
# flush output here to force SIGPIPE to be triggered
# while inside this try block.
sys.stdout.flush()
except BrokenPipeError:
# Python flushes standard streams on exit; redirect remaining output
# to devnull to avoid another BrokenPipeError at shutdown
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, sys.stdout.fileno())
sys.exit(1) # Python exits with error code 1 on EPIPE
if __name__ == '__main__':
main()
Do not set :const:`SIGPIPE`'s disposition to :const:`SIG_DFL`
in order to avoid :exc:`BrokenPipeError`. Doing that would cause
your program to exit unexpectedly also whenever any socket connection
is interrupted while your program is still writing to it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment