Python signal.SIGPIPE Examples

The following are 16 code examples of signal.SIGPIPE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module signal , or try the search function .
Example #1
Source File: __main__.py    From twitter_markov with GNU General Public License v3.0 6 votes vote down vote up
def learn_func(**kwargs):
    if not kwargs['quiet']:
        print("Reading " + kwargs['archive'], file=sys.stderr)

    archive = tbu.archive.read_csv(kwargs.get('archive'))
    gen = checking.generator(archive, **kwargs)
    tweets = (tweet.replace(u'\n', u' ') + '\n' for tweet in gen)

    if kwargs['output'] in ('-', '/dev/stdout'):
        signal(SIGPIPE, SIG_DFL)
        sys.stdout.writelines(tweets)

    else:
        if not kwargs['quiet']:
            print("Writing " + kwargs['output'], file=sys.stderr)

        with open(kwargs.get('output'), 'w') as f:
            f.writelines(tweets) 
Example #2
Source File: pipeline.py    From Comparative-Annotation-Toolkit with Apache License 2.0 5 votes vote down vote up
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) 
Example #3
Source File: pipeline.py    From Comparative-Annotation-Toolkit with Apache License 2.0 5 votes vote down vote up
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() 
Example #4
Source File: autograder.py    From autograder with GNU General Public License v3.0 5 votes vote down vote up
def signal_to_string(self, signalNumber):
        if signalNumber < 0:
            signalNumber = signalNumber * -1

        if signalNumber == signal.SIGINT:
            return "SIGINT - Interrupt (Ctrl+C)"
        elif signalNumber == signal.SIGKILL:
            return "SIGKILL - Killed"
        elif signalNumber == signal.SIGTERM:
            return "SIGTERM - Terminated"
        elif signalNumber == signal.SIGSEGV:
            return "SIGSEGV - Segmentation fault"
        elif signalNumber == signal.SIGHUP:
            return "SIGHUP - Hang up"
        elif signalNumber == signal.SIGBUS:
            return "SIGBUS - Bus error"
        elif signalNumber == signal.SIGILL:
            return "SIGILL - Illegal instruction"
        elif signalNumber == signal.SIGFPE:
            return "SIGFPE - Floating point exception"
        elif signalNumber == signal.SIGPIPE:
            return "SIGPIPE - Broken pipe (write to pipe with no readers)"
        elif signalNumber == signal.SIGABRT:
            return "SIGABRT - Called abort()"
        elif signalNumber == signal.SIGXFSZ:
            return "SIGXFSZ - Process created files that were too big."
        elif signalNumber == signal.SIGXCPU:
            return "SIGXCPU - Process used too much CPU time."
        else:
            return "Unknown signal #" + str(signalNumber) 
Example #5
Source File: sh.py    From scylla with Apache License 2.0 5 votes vote down vote up
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 
Example #6
Source File: fuse_runner.py    From parsec-cloud with GNU Affero General Public License v3.0 5 votes vote down vote up
def _reset_signals(signals=None):
    """A context that save the current signal handlers restore them when leaving.

    By default, it does so for SIGINT, SIGTERM, SIGHUP and SIGPIPE.
    """
    if signals is None:
        signals = (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGPIPE)
    saved = {sig: ctypes.pythonapi.PyOS_getsig(sig) for sig in signals}
    try:
        yield
    finally:
        for sig, handler in saved.items():
            if ctypes.pythonapi.PyOS_getsig(sig) != handler:
                ctypes.pythonapi.PyOS_setsig(sig, handler) 
Example #7
Source File: start_gdb.py    From dcc with GNU General Public License v3.0 5 votes vote down vote up
def kill(pid, which_signal=None):
#	print('killing', pid)
	try:
		if which_signal is None:
			#in some circumstance SIGPIPE can avoid killed message
			os.kill(pid, signal.SIGPIPE) 
			os.kill(pid, signal.SIGKILL)
		else:
			os.kill(pid, which_signal)
	except ProcessLookupError:
		pass 
Example #8
Source File: get_qnas.py    From convai-bot-1337 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect("tcp://127.0.0.1:5556") 
Example #9
Source File: get_reply.py    From convai-bot-1337 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, url):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect(url) 
Example #10
Source File: get_reply.py    From convai-bot-1337 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, url):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect(url) 
Example #11
Source File: get_qnas.py    From convai-bot-1337 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect("tcp://opennmt:5556") 
Example #12
Source File: get_reply.py    From convai-bot-1337 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, url):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect(url) 
Example #13
Source File: get_qnas.py    From question_generation with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        signal(SIGPIPE, SIG_DFL)
        self.sock = zmq.Context().socket(zmq.REQ)
        self.sock.connect("tcp://127.0.0.1:5556") 
Example #14
Source File: command.py    From shell-functools with MIT License 5 votes vote down vote up
def configure_broken_pipe():
        # Use the default behavior (exit quietly) when catching SIGPIPE
        signal(SIGPIPE, SIG_DFL) 
Example #15
Source File: sh.py    From scylla with Apache License 2.0 4 votes vote down vote up
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, _ = is_alive()
    while alive:
        quit.wait(1)
        alive, _ = is_alive()

    if stdout:
        stdout.close()

    if stderr:
        stderr.close() 
Example #16
Source File: cli.py    From certstream-python with MIT License 4 votes vote down vote up
def main():
    args = parser.parse_args()

    # Ignore broken pipes
    signal(SIGPIPE, SIG_DFL)

    log_level = logging.INFO
    if args.verbose:
        log_level = logging.DEBUG

    logging.basicConfig(format='[%(levelname)s:%(name)s] %(asctime)s - %(message)s', level=log_level)

    def _handle_messages(message, context):
        if args.json:
            sys.stdout.flush()
            sys.stdout.write(json.dumps(message) + "\n")
            sys.stdout.flush()
        else:
            if args.disable_colors:
                logging.debug("Starting normal output.")
                payload = "{} {} - {} {}\n".format(
                    "[{}]".format(datetime.datetime.fromtimestamp(message['data']['seen']).isoformat()),
                    message['data']['source']['url'],
                    message['data']['leaf_cert']['subject']['CN'],
                    "[{}]".format(", ".join(message['data']['leaf_cert']['all_domains'])) if args.full else ""
                )

                sys.stdout.write(payload)
            else:
                logging.debug("Starting colored output.")
                payload = "{} {} - {} {}\n".format(
                    termcolor.colored("[{}]".format(datetime.datetime.fromtimestamp(message['data']['seen']).isoformat()), 'cyan', attrs=["bold", ]),
                    termcolor.colored(message['data']['source']['url'], 'blue', attrs=["bold",]),
                    termcolor.colored(message['data']['leaf_cert']['subject']['CN'], 'green', attrs=["bold",]),
                    termcolor.colored("[", 'blue') + "{}".format(
                        termcolor.colored(", ", 'blue').join(
                            [termcolor.colored(x, 'white', attrs=["bold",]) for x in message['data']['leaf_cert']['all_domains']]
                        )
                    ) + termcolor.colored("]", 'blue') if args.full else "",
                )
                sys.stdout.write(payload)

            sys.stdout.flush()

    certstream.listen_for_events(_handle_messages, args.url, skip_heartbeats=True)