Python gevent.signal() Examples

The following are 30 code examples of gevent.signal(). 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 gevent , or try the search function .
Example #1
Source File: test_gipc.py    From gipc with MIT License 6 votes vote down vote up
def test_exitcode_previous_to_join(self):
        p = start_process(lambda: gevent.sleep(SHORTTIME))
        # Assume that the child process is still alive when the next
        # line is executed by the interpreter (there is no guarantee
        # for that, but it's rather likely).
        assert p.exitcode is None

        # Expect the child watcher mechanism to pick up
        # and process the child process termination event
        # (within at most two seconds). The `gevent.sleep()`
        # invocations allow for libev event loop iterations,
        # two of which are required after the OS delivers the
        # SIGCHLD signal to the parent process: one iteration
        # invokes the child reap loop, and the next invokes
        # the libev callback associated with the termination
        # event.
        deadline = time.time() + 2
        while time.time() < deadline:
            if p.exitcode is not None:
                assert p.exitcode == 0
                p.join()
                return
            gevent.sleep(ALMOSTZERO)
        raise Exception('Child termination not detected') 
Example #2
Source File: rq_gevent_worker.py    From Dallinger with MIT License 6 votes vote down vote up
def _install_signal_handlers(self):
        def request_force_stop():
            self.log.warning("Cold shut down.")
            self.gevent_pool.kill()
            raise SystemExit()

        def request_stop():
            if not self._stop_requested:
                gevent.signal(signal.SIGINT, request_force_stop)
                gevent.signal(signal.SIGTERM, request_force_stop)

                self.log.warning("Warm shut down requested.")
                self.log.warning(
                    "Stopping after all greenlets are finished. "
                    "Press Ctrl+C again for a cold shutdown."
                )

                self._stop_requested = True
                self.gevent_pool.join()
                if self.gevent_worker is not None:
                    self.gevent_worker.kill(StopRequested)

        gevent.signal(signal.SIGINT, request_stop)
        gevent.signal(signal.SIGTERM, request_stop) 
Example #3
Source File: test_gipc.py    From gipc with MIT License 6 votes vote down vote up
def test_early_readchild_exit_write_from_child(self):
        pr = start_process(ipc_readonce_then_exit, (self.rh,))
        pw = start_process(ipc_endless_write_for_early_reader_exit, (self.wh,))
        # This test is to make sure equivalent behavior as in test
        # `test_early_readchild_exit` when the writing process is a
        # child process itself (above, the write process in the initial
        # process). Since gipc's child process creation
        # routine messes around with signal handlers, this test makes
        # sure that SIGPIPE is ignored in the child and that a
        # failing write attempt (after early read child exit) results
        # in an exception raised in the writing process.
        pr.join()
        pw.join()
        assert pr.exitcode == 0
        assert pw.exitcode == 0
        self.rh2.close()
        self.wh2.close() 
Example #4
Source File: monitor_network.py    From powerpool with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def start(self):
        Jobmanager.start(self)

        if self.config['signal']:
            self.logger.info("Listening for push block notifs on signal {}"
                             .format(self.config['signal']))
            gevent.signal(self.config['signal'], self.getblocktemplate, signal=True)

        # Find desired auxmonitors
        self.config['merged'] = set(self.config['merged'])
        found_merged = set()

        for mon in self.manager.component_types['Jobmanager']:
            if mon.key in self.config['merged']:
                self.auxmons.append(mon)
                found_merged.add(mon.key)
                mon.new_job.rawlink(self.new_merged_work)

        for monitor in self.config['merged'] - found_merged:
            self.logger.error("Unable to locate Auxmonitor(s) '{}'".format(monitor)) 
Example #5
Source File: monitor_network.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, config):
        NodeMonitorMixin.__init__(self)
        self._configure(config)
        if get_bcaddress_version(self.config['pool_address']) is None:
            raise ConfigurationError("No valid pool address configured! Exiting.")

        # Since some MonitorNetwork objs are polling and some aren't....
        self.gl_methods = ['_monitor_nodes', '_check_new_jobs']

        # Aux network monitors (merged mining)
        self.auxmons = []

        # internal vars
        self._last_gbt = {}
        self._job_counter = 0  # a unique job ID counter

        # Currently active jobs keyed by their unique ID
        self.jobs = {}
        self.stale_jobs = deque([], maxlen=10)
        self.latest_job = None  # The last job that was generated
        self.new_job = Event()
        self.last_signal = 0.0

        # general current network stats
        self.current_net = dict(difficulty=None,
                                height=None,
                                last_block=0.0,
                                prev_hash=None,
                                transactions=None,
                                subsidy=None)
        self.block_stats = dict(accepts=0,
                                rejects=0,
                                solves=0,
                                last_solve_height=None,
                                last_solve_time=None,
                                last_solve_worker=None)
        self.recent_blocks = deque(maxlen=15)

        # Run the looping height poller if we aren't getting push notifications
        if (not self.config['signal'] and self.config['poll'] is None) or self.config['poll']:
            self.gl_methods.append('_poll_height') 
Example #6
Source File: console_service.py    From pyethapp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, app):
        super(Console, self).__init__(app)
        self.interrupt = Event()
        gevent.signal(signal.SIGTSTP, self.interrupt.set)
        self.console_locals = [] 
Example #7
Source File: app.py    From chainerui with MIT License 5 votes vote down vote up
def server_handler(args):
    """server_handler."""
    if not db.setup(url=args.db, echo=args.db_echo):
        return
    if not _check_db_revision():
        return

    app = create_app()
    listener = '{:s}:{:d}'.format(args.host, args.port)
    if args.debug:
        logging.getLogger('werkzeug').disabled = True
        set_loglevel(logging.DEBUG)
        app.config['ENV'] = 'development'
        app.debug = True

        _show_banner_debug(app, listener)
        from werkzeug.serving import run_simple
        run_simple(
            args.host, args.port, app, use_reloader=True, use_debugger=True,
            threaded=True)
    else:
        app.config['ENV'] = 'production'
        import gevent
        from gevent.pywsgi import WSGIServer
        http_server = WSGIServer(listener, application=app, log=None)

        def stop_server():
            if http_server.started:
                http_server.stop()

        gevent.signal(signal.SIGTERM, stop_server)
        gevent.signal(signal.SIGINT, stop_server)
        logger.info(' * Environment: {}'.format(app.config['ENV']))
        logger.info(' * Running on http://{}/ (Press CTRL+C to quit)'.format(
            listener))

        try:
            http_server.serve_forever()
        except (KeyboardInterrupt, SystemExit):
            stop_server() 
Example #8
Source File: daemon.py    From Telebix with GNU General Public License v3.0 5 votes vote down vote up
def stop(self):
        """
        Stop the daemon
        """

        if self.verbose >= 1:
            pass

        # Get the pid from the pidfile
        pid = self.get_pid()

        if not pid:

            if os.path.exists(self.pidfile):
                os.remove(self.pidfile)

            return  # Not an error in a restart

        # Try killing the daemon process
        try:
            i = 0
            while 1:
                os.kill(pid, signal.SIGTERM)
                time.sleep(0.1)
                i = i + 1
                if i % 10 == 0:
                    os.kill(pid, signal.SIGHUP)
        except OSError as err:
            if err.errno == errno.ESRCH:
                if os.path.exists(self.pidfile):
                    os.remove(self.pidfile)
            else:
                print(str(err))
                sys.exit(1)

        pass 
Example #9
Source File: sender.py    From iris with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def sender_shutdown():
    global shutdown_started

    # Make control+c or some other kill signal force quit the second time it happens
    if shutdown_started:
        logger.info('Force exiting')
        os._exit(0)
    else:
        shutdown_started = True
        logger.info('Shutting server..')

    # Immediately release all locks and give up any master status and slave presence
    if coordinator:
        coordinator.leave_cluster()

    # Stop sender RPC server
    rpc.shutdown()

    for tasks in worker_tasks.values():
        for task in tasks:
            task['kill_set'].set()

    for tasks in autoscale_email_worker_tasks.values():
        for task in tasks:
            task['kill_set'].set()

    logger.info('Waiting for sender workers to shut down')

    for tasks in worker_tasks.values():
        for task in tasks:
            task['greenlet'].join()

    for tasks in autoscale_email_worker_tasks.values():
        for task in tasks:
            task['greenlet'].join()

    # Force quit. Avoid sender process existing longer than it needs to
    os._exit(0) 
Example #10
Source File: portforwarder.py    From web_develop with GNU General Public License v3.0 5 votes vote down vote up
def main():
    args = sys.argv[1:]
    if len(args) != 2:
        sys.exit('Usage: %s source-address destination-address' % __file__)
    source = args[0]
    dest = parse_address(args[1])
    server = PortForwarder(source, dest)
    log('Starting port forwarder %s:%s -> %s:%s', *(server.address[:2] + dest))
    gevent.signal(signal.SIGTERM, server.close)
    gevent.signal(signal.SIGINT, server.close)
    server.start()
    gevent.wait() 
Example #11
Source File: zk_register.py    From kingpin with Apache License 2.0 5 votes vote down vote up
def sigusr1_handler():
    print 'Received SIGUSER1 -- Graceful exit'
    sys.exit(0)

# Set the signal handler 
Example #12
Source File: main.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def exit(self, signal=None):
        """ Handle an exit request """
        self.logger.info("{} {}".format(signal, "*" * 80))
        # Kill the top level greenlet
        gevent.kill(gevent.hub.get_hub().parent) 
Example #13
Source File: gateway_application_service.py    From WavesGatewayFramework with MIT License 5 votes vote down vote up
def run(self) -> None:
        """
        Starts all poller instances.
        After that, polling is performed in regular intervals specified by the polling_delay_ms property.
        By default this function blocks the current thread.
        """
        self._validation_service.validate_all_addresses()

        self._logger.info("Gateway Application started")

        task_group = gevent.pool.Group()

        gevent.signal(signal.SIGINT, self._coin_transaction_polling_service.cancel)
        gevent.signal(signal.SIGINT, self._waves_transaction_polling_service.cancel)

        task_group.start(self._coin_transaction_polling_service)
        task_group.start(self._waves_transaction_polling_service)

        attempt_list_workers = self._create_attempt_list_workers()

        for worker in attempt_list_workers:
            gevent.signal(signal.SIGINT, worker.cancel)
            task_group.start(worker)

        http = gevent.pywsgi.WSGIServer(
            (self._host, self._port), self._flask, log=gevent.pywsgi.LoggingLogAdapter(self._logger.getChild('pywsgi')))

        gevent.signal(signal.SIGINT, http.close)

        self._logger.info('Listening on %s:%s', self._host, str(self._port))

        http.serve_forever()

        task_group.join(raise_error=True) 
Example #14
Source File: monitor_network.py    From powerpool with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def getblocktemplate(self, new_block=False, signal=False):
        if signal:
            self.last_signal = time.time()
        try:
            # request local memory pool and load it in
            bt = self.call_rpc('getblocktemplate',
                               {'capabilities': [
                                   'coinbasevalue',
                                   'coinbase/append',
                                   'coinbase',
                                   'generation',
                                   'time',
                                   'transactions/remove',
                                   'prevblock',
                               ]})
        except RPCException:
            return False

        if self._last_gbt.get('height') != bt['height']:
            new_block = True
        # If this was from a push signal and the
        if signal and new_block:
            self.logger.info("Push block signal notified us of a new block!")
        elif signal:
            self.logger.info("Push block signal notified us of a block we "
                             "already know about!")
            return

        # generate a new job if we got some new work!
        dirty = False
        if bt != self._last_gbt:
            self._last_gbt = bt
            self._last_gbt['update_time'] = time.time()
            dirty = True

        if new_block or dirty:
            # generate a new job and push it if there's a new block on the
            # network
            self.generate_job(push=new_block, flush=new_block, new_block=new_block) 
Example #15
Source File: daemon.py    From Saker with GNU General Public License v3.0 5 votes vote down vote up
def stop(self):
        """
        Stop the daemon
        """
        if self.verbose >= 1:
            self.log("Stopping...")

        # Get the pid from the pidfile
        pid = self.getpid()

        if not pid:
            message = "pidfile %s does not exist. Not running?\n"
            sys.stderr.write(message % self.pidfile)
            if os.path.exists(self.pidfile):
                os.remove(self.pidfile)
            return

        # Try killing the daemon process
        try:
            i = 0
            while 1:
                os.kill(pid, signal.SIGTERM)
                time.sleep(0.1)
                i += 1
                if i % 10 == 0:
                    os.kill(pid, signal.SIGHUP)
        except OSError as err:
            if err.errno == errno.ESRCH:
                if os.path.exists(self.pidfile):
                    os.remove(self.pidfile)
            else:
                print(str(err))
                sys.exit(1)
        self.log("Stopped") 
Example #16
Source File: nike_terminator.py    From nike_purchase_system with GNU General Public License v3.0 5 votes vote down vote up
def run_gevent(self):
        # 程序如果意外退出,协程部分不会被kill掉,需要下面这一行代码来关联信号量
        # gevent.signal(signal.SIGQUIT, gevent.shutdown)
        # 构建一个和账号数量相同的协程池
        coroutine_pool = gevent.pool.Pool(len(self._accounts))
        self.__sessions = [requests.session() for _ in range(len(self.accounts))]
        self.__user_ids = [None for _ in range(len(self.accounts))]
        self.__buy_status = [0 for _ in range(len(self.accounts))]
        
        # 传入账号的序号和账号
        for account_id, account in enumerate(self._accounts):
            coroutine_pool.apply_async(self.do, (account_id, account, self.sizes[account_id], self.proxies[account_id]))
        coroutine_pool.join() 
Example #17
Source File: main.py    From opennumber with GNU General Public License v3.0 5 votes vote down vote up
def handler_signal_ctrl_c():
    pid = os.getpid()
    os.killpg(os.getpgid(pid), signal.SIGKILL)
    os.kill(pid, signal.SIGKILL)
    return  # 
Example #18
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def test_exitcode_sigkill(self):
        p = start_process(p_child_b)
        p.join()
        if not WINDOWS:
            assert p.exitcode == -signal.SIGKILL
        else:
            assert p.exitcode == 1
        _call_close_method_if_exists(p) 
Example #19
Source File: server.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_rpc(interface_dict):
	print("MpRPC server Started.")
	server_instance = FetchInterfaceClass(interface_dict, "MpRPC")
	mprpc_server = StreamServer(('0.0.0.0', 4315), server_instance)

	gevent.signal(signal.SIGINT, build_mprpc_handler(mprpc_server))
	mprpc_server.serve_forever() 
Example #20
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def test_terminate(self):
        p = start_process(gevent.sleep, args=(1,))
        # Test __repr__ and __str__
        p.__repr__()
        p.terminate()
        p.join()
        p.__repr__()
        assert p.exitcode == -signal.SIGTERM
        _call_close_method_if_exists(p) 
Example #21
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def p_child_b():
    if not WINDOWS:
        os.kill(os.getpid(), signal.SIGKILL)
    else:
        sys.exit(1) 
Example #22
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def teardown(self):
        check_for_handles_left_open()
        # One could verify that signal handlers are not left improperly
        # by a test case, but libev's signal handling might go through
        # signalfd() which we cannot detect here anyway. So the test cases
        # have to properly clean up their signal handling modifications
        # themselves. 
Example #23
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def test_orphaned_signal_watcher(self):
        # Install libev-based signal watcher.
        try:
            s = gevent.signal(signal.SIGTERM, signals_test_sigterm_handler)
        except AttributeError:
            # This function got renamed in gevent 1.5
            s = gevent.signal_handler(signal.SIGTERM, signals_test_sigterm_handler)
        # Normal behavior: signal handlers become inherited by children.
        # Bogus behavior of libev-based signal watchers in child process:
        # They should not be active anymore when 'orphaned' (when their
        # corresponding event loop has been destroyed). What happens, however:
        # The old handler stays active and registering a new handler does not
        # 'overwrite' the old one -- both are active.
        # Since this test is about testing the behavior of 'orphaned' libev
        # signal watchers, the signal must be transmitted *after* event loop
        # recreation, so wait here for the child process to go through
        # the hub & event loop destruction (and recreation) process before
        # sending the signal. Waiting is realized with sync through pipe.
        # Without cleanup code in gipc, the inherited but orphaned libev signal
        # watcher would be active in the fresh event loop and trigger the
        # handler. This is a problem. With cleanup code, this handler must
        # never be called. Child exitcode 20 means that the inherited handler
        # has been called, -15 (-signal.SIGTERM) means that the child was
        # actually killed by SIGTERM within a certain short time interval.
        # Returncode 0 would mean that the child finished normally after that
        # short time interval.
        with pipe() as (r, w):
            p = start_process(signals_test_child_a, (w,))
            assert r.get() == p.pid
            os.kill(p.pid, signal.SIGTERM)
            p.join()
            if not WINDOWS:
                assert p.exitcode == -signal.SIGTERM
            else:
                assert p.exitcode == signal.SIGTERM
        s.cancel() 
Example #24
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def test_signal_handlers_default(self):
        p = start_process(signals_test_child_defaulthandlers)
        p.join()
        # Child exits normally when all signal dispositions are default.
        assert p.exitcode == 0 
Example #25
Source File: test_gipc.py    From gipc with MIT License 5 votes vote down vote up
def signals_test_child_defaulthandlers():
    for s in signals_to_reset:
        assert signal.getsignal(s) is signal.SIG_DFL 
Example #26
Source File: server.py    From ReadableWebProxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def build_mprpc_handler(server):
	global TO_EXIT
	TO_EXIT.append(server)

	def handler(signum=-1, frame=None):
		global INTERRUPTS
		INTERRUPTS += 1
		print('Signal handler called with signal %s for the %s time' % (signum, INTERRUPTS))
		if INTERRUPTS > 2:
			print("Raising due to repeat interrupts")
			raise KeyboardInterrupt
		for server in TO_EXIT:
			server.close()
	return handler 
Example #27
Source File: ServerRack.py    From OpenMTC with Eclipse Public License 1.0 5 votes vote down vote up
def serve_forever(self):
        gevent.signal(signal.SIGTERM, self.stop)
        gevent.signal(signal.SIGINT, self.stop)
        self.start()
        self._shutdown_event.wait() 
Example #28
Source File: launcher.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def _run_chassis(fabricconfig, mgmtbusconfig, fts):
    try:
        # lower priority to make master and web
        # more "responsive"
        os.nice(5)

        c = minemeld.chassis.Chassis(
            fabricconfig['class'],
            fabricconfig['config'],
            mgmtbusconfig
        )
        c.configure(fts)

        gevent.signal(signal.SIGUSR1, c.stop)

        while not c.fts_init():
            if c.poweroff.wait(timeout=0.1) is not None:
                break

            gevent.sleep(1)

        LOG.info('Nodes initialized')

        try:
            c.poweroff.wait()
            LOG.info('power off')

        except KeyboardInterrupt:
            LOG.error("We should not be here !")
            c.stop()

    except:
        LOG.exception('Exception in chassis main procedure')
        raise 
Example #29
Source File: console.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def cli(ctx, verbose, comm_class):
    comm_class = str(comm_class)
    source = 'console-%d' % int(time.time())

    loglevel = logging.WARNING
    if verbose > 0:
        loglevel = logging.INFO
    if verbose > 1:
        loglevel = logging.DEBUG
    logging.basicConfig(
        level=loglevel,
        format="%(asctime)s (%(process)d)%(module)s.%(funcName)s"
               " %(levelname)s: %(message)s",
        datefmt="%Y-%m-%dT%H:%M:%S"
    )

    comm = minemeld.comm.factory(comm_class, {})  # XXX should support config

    gevent.signal(signal.SIGTERM, comm.stop)
    gevent.signal(signal.SIGQUIT, comm.stop)
    gevent.signal(signal.SIGINT, comm.stop)

    comm.start()

    ctx.obj['COMM'] = comm
    ctx.obj['SOURCE'] = source 
Example #30
Source File: console.py    From minemeld-core with Apache License 2.0 5 votes vote down vote up
def mm_signal(ctx, signal, target):
    target = 'mbus:directslave:'+target
    _print_json(_send_cmd(ctx, target, 'signal', source=False, params={'signal': signal}))

    ctx.obj['COMM'].stop()


# XXX query should subscribe to the Redis topic to dump the
# query results