![]() ![]() # the script aborts quietly, and the process exit code is set to # ONLY SUITABLE FOR COMMAND-LINE UTILITIESįrom signal import signal, SIGPIPE, SIG_DFL However, if your script is a command-line utility, where quiet termination may not only be acceptable but preferred so as to play nicely with the standard head utility, for instance, you can abort quietly as follows, using signal.signal() to install the platform's default signal handler (which behaves as described above), as also shown in akhan's answer (works in both Python 3 and 2):.Generally, it is not advisable to silence this exception, as it may signal a severe error condition, depending on your script's purpose, such as the receiving end of a network socket unexpectedly closing. There are two ways to solve this problem: If your script runs in a Unix subsystem on Windows, the SIGPIPE signal may surface differently than on Unix - see this answer. If your script needs to run directly on Windows too, you may have to conditionally bypass code that references SIGPIPE, as shown in this answer. Windows considerations ( SIGPIPE is a Unix-only signal) If a Python script does not catch the exception, Python outputs error message BrokenPipeError: Broken pipe ( Python 3, possibly twice, with Exception ignored in: ' mode='w' encoding='utf-8'> sandwiched in between) / IOError: Broken pipe ( Python 2) and terminates the script with exit code 1 - this is the symptom Johannes (the OP) saw. Note: If you use a Unix emulation environment on Windows, the error may surface differently - see this answer.However, by design Python itself traps SIGPIPE and translates it into a Python BrokenPipeError (Python 3) / IOError (Python 2) instance with errno value errno.EPIPE. python -c 'for x in range(10000): print(x)' | head -n 1īy default - i.e., if the writing process does not explicitly trap SIGPIPE - the writing process is simply terminated, and its exit code is set to 141, which is calculated as 128 (to signal termination by signal in general) + 13 ( SIGPIPE's specific signal number).Therefore, an easy way to provoke this error is to pipe to head e.g.:.This is not necessarily an error condition some Unix utilities such as head by design stop reading prematurely from a pipe, once they've received enough data.Standard Unix signal SIGPIPE is sent to a process writing to a pipe when there's no process reading from the pipe (anymore). To bring information from the many helpful answers together, with some additional information: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |