我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用signal.SIGPIPE。
def __init__(self): # process command line options and load config files self._config = Config() self._threads = [] self._exiting = False self._reload = False # signal handling for sig, action in ( (signal.SIGINT, self.shutdown), (signal.SIGQUIT, self.shutdown), (signal.SIGTERM, self.shutdown), (signal.SIGHUP, lambda s, f: setattr(self, '_reload', True)), (signal.SIGPIPE, signal.SIG_IGN), ): try: signal.signal(sig, action) except AttributeError: pass log.trace(self._config)
def safe_system(command, catch_sigpipe=False): popen = Popen( args = safe_popen_args(command), shell = False, preexec_fn = lambda: signal(SIGPIPE, SIG_DFL), ) status = popen.wait() # pylint: disable-msg=E1101 if status == 0: return status # ??? OK if catch_sigpipe: if status & 128 == 128 and status & 127 == SIGPIPE: return status # bash ??????? ???, ??? ??? ????? ?????? SIGPIPE elif status < 0 and -status == SIGPIPE: return status # bash ????? ?????? SIGPIPE # ?????? bash ?????? fork/exec ?????? ?????? exec (???????????, ?? ?????? # ????? ????? ????????? ???? ??????? ???????) ? ????? ??????? ??? ???????? # ???, ??? ????? ?????? ?????? ???, ???? ?? ????? ???? ???? ??? ????? ???. # ?????-?? ?????? (???????? ?? SIGPIPE ??? catch_sigpipe=True) raise SafePopenError(command, status)
def run_pipeline(cmds): # The Python executable (and its children) ignore SIGPIPE. (See # http://bugs.python.org/issue1652) Our subprocesses need to see # it. sigpipe_handler = signal.signal(signal.SIGPIPE, signal.SIG_DFL) stdin = None last_proc = None procs = [] try: for cmd in cmds: proc = subprocess.Popen(shlex.split(cmd), stdin=stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if last_proc: # Ensure last_proc receives SIGPIPE if proc exits first last_proc.stdout.close() procs.append(proc) stdin = proc.stdout last_proc = proc finally: signal.signal(signal.SIGPIPE, sigpipe_handler) last_stderr = last_proc.communicate()[1] results = [] for (cmd, proc) in zip(cmds[:-1], procs[:-1]): # wait() is OK here, despite use of PIPE above; these procs # are finished. proc.wait() results.append((proc.returncode, proc.stderr.read())) results.append((last_proc.returncode, last_stderr)) return results
def __doChildStart(self): "guts of start child process" self.statusPipe.postForkChild() _setPgid(os.getpid(), self.dag.pgid if (self.dag.pgid is not None) else os.getpid()) cmd = self.__buildCmd() self.__stdioSetup(self.stdin, 0) self.__stdioSetup(self.stdout, 1) self.__stdioSetup(self.stderr, 2) self.__closeFiles() signal.signal(signal.SIGPIPE, signal.SIG_DFL) os.execvp(cmd[0], cmd)
def _handleExit(self, waitStat): """Handle process exiting, saving status Call close on all PInOut objects to disassociate """ self.finished = True assert(os.WIFEXITED(waitStat) or os.WIFSIGNALED(waitStat)) self.returncode = os.WEXITSTATUS(waitStat) if os.WIFEXITED(waitStat) else -os.WTERMSIG(waitStat) if not ((self.returncode == 0) or (self.returncode == -signal.SIGPIPE)): self.__handleErrExit() for pin in self.pins: pin.close() for pout in self.pouts: pout.close()
def get_exc_exit_code_would_raise(exit_code, ok_codes, sigpipe_ok): exc = None success = exit_code in ok_codes bad_sig = -exit_code in SIGNALS_THAT_SHOULD_THROW_EXCEPTION # if this is a piped command, SIGPIPE must be ignored by us and not raise an # exception, since it's perfectly normal for the consumer of a process's # pipe to terminate early if sigpipe_ok and -exit_code == signal.SIGPIPE: bad_sig = False success = True if not success or bad_sig: exc = get_rc_exc(exit_code) return exc
def __init__(self, cmdline, bufsize=None, stdin=None, stdout=PIPE): popen_args = dict( args = safe_popen_args(cmdline), shell = False, stdin = stdin, stdout = stdout, preexec_fn = lambda: signal(SIGPIPE, SIG_DFL), ) if bufsize != None: popen_args['bufsize'] = bufsize super(SafePopen, self).__init__(**popen_args) self.__cmdline = cmdline
def __init__(self, game_filename): signal(SIGPIPE, SIG_DFL) self.game_loaded_properly = True # Verify that specified game file exists, else limit functionality if game_filename == None or not os.path.exists('textplayer/games/' + game_filename): self.game_loaded_properly = False print "Unrecognized game file or bad path" return self.game_filename = game_filename self.game_log = game_filename + '_log.txt' self.debug = False # Runs the game
def check_valid_returncode(self, valid : Container[int] = ( 0, None, signal.SIGPIPE, signal.SIGPIPE + 128)): """Check that the returncodes does not have a value associated with an error state. Raises: IOError if :attribute:`returncode` is associated with an error state. """ if self.returncode not in valid: raise IOError("Process existed with return code {}".format( self.returncode)) # Methods
def _prefunc(): # pragma: no-cover """Handle a SIGPIPE error in Popen (happens when calling a command that has pipes). """ signal.signal(signal.SIGPIPE, signal.SIG_DFL)
def main(args): parser = argparse.ArgumentParser( description='Dump relations from a database') parser.add_argument(dest='url', help="database url") # Ignore SIG_PIPE and don't throw exceptions on it signal(SIGPIPE, SIG_DFL) args = parser.parse_args(args) database = load(args.url) database.schema.dump_relations(sys.stdout) database.disconnect()
def __init__(self): signal(SIGPIPE, SIG_DFL) self.sock = zmq.Context().socket(zmq.REQ) self.sock.connect("tcp://127.0.0.1:5556")
def transcode(flac_file, output_dir, output_format): ''' Transcodes a FLAC file into another format. ''' # gather metadata from the flac file flac_info = mutagen.flac.FLAC(flac_file) sample_rate = flac_info.info.sample_rate bits_per_sample = flac_info.info.bits_per_sample resample = sample_rate > 48000 or bits_per_sample > 16 # if resampling isn't needed then needed_sample_rate will not be used. needed_sample_rate = resample_rate(os.path.dirname(flac_file)) if resample and needed_sample_rate is None: raise UnknownSampleRateException( 'FLAC file "{0}" has a sample rate {1}, which is not 88.2,', '176.4, or 96kHz, but needs resampling. This is unsupported.'.format(flac_file, sample_rate) ) if flac_info.info.channels > 2: raise TranscodeDownmixException('FLAC file "%s" has more than 2 channels, unsupported' % flac_file) # determine the new filename transcode_basename = os.path.splitext(os.path.basename(flac_file))[0] transcode_basename = re.sub(r'[\?<>\\*\|"]', '_', transcode_basename) transcode_file = os.path.join(output_dir, transcode_basename) transcode_file += ENCODERS[output_format]['ext'] if not os.path.exists(os.path.dirname(transcode_file)): try: os.makedirs(os.path.dirname(transcode_file)) except OSError as e: if e.errno == errno.EEXIST: # Harmless race condition -- another transcode process # beat us here. pass else: raise e commands = transcode_commands(output_format, resample, needed_sample_rate, flac_file, transcode_file) results = run_pipeline(commands) # Check for problems. Because it's a pipeline, the earliest one is # usually the source. The exception is -SIGPIPE, which is caused # by "backpressure" due to a later command failing: ignore those # unless no other problem is found. last_sigpipe = None for (cmd, (code, stderr)) in zip(commands, results): if code: if code == -signal.SIGPIPE: last_sigpipe = (cmd, (code, stderr)) else: raise TranscodeException('Transcode of file "%s" failed: %s' % (flac_file, stderr)) if last_sigpipe: # XXX: this should probably never happen.... raise TranscodeException('Transcode of file "%s" failed: SIGPIPE' % flac_file) return transcode_file
def output_thread(log, stdout, stderr, timeout_event, is_alive, quit, stop_output_event): """ this function is run in a separate thread. it reads from the process's stdout stream (a streamreader), and waits for it to claim that its done """ poller = Poller() if stdout is not None: poller.register_read(stdout) if stderr is not None: poller.register_read(stderr) # this is our poll loop for polling stdout or stderr that is ready to # be read and processed. if one of those streamreaders indicate that it # is done altogether being read from, we remove it from our list of # things to poll. when no more things are left to poll, we leave this # loop and clean up while poller: changed = no_interrupt(poller.poll, 0.1) for f, events in changed: if events & (POLLER_EVENT_READ | POLLER_EVENT_HUP): log.debug("%r ready to be read from", f) done = f.read() if done: poller.unregister(f) elif events & POLLER_EVENT_ERROR: # for some reason, we have to just ignore streams that have had an # error. i'm not exactly sure why, but don't remove this until we # figure that out, and create a test for it pass if timeout_event and timeout_event.is_set(): break if stop_output_event.is_set(): break # we need to wait until the process is guaranteed dead before closing our # outputs, otherwise SIGPIPE alive = True while alive: quit.wait(1) alive, _ = is_alive() if stdout: stdout.close() if stderr: stderr.close()