当前位置: 首页>>代码示例>>Python>>正文


Python Monitor.children_count方法代码示例

本文整理汇总了Python中monitor.Monitor.children_count方法的典型用法代码示例。如果您正苦于以下问题:Python Monitor.children_count方法的具体用法?Python Monitor.children_count怎么用?Python Monitor.children_count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在monitor.Monitor的用法示例。


在下文中一共展示了Monitor.children_count方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Daemon

# 需要导入模块: from monitor import Monitor [as 别名]
# 或者: from monitor.Monitor import children_count [as 别名]
class Daemon(Registry):
    _class_id = 'Daemon'
    _singleton = True

    clients = {}

    def __reginit__(self, daemon, sock):
        self.imq = {}
        self.monitor = Monitor(input_message_queue=self.imq)
        self._run_server(daemon, sock)

    def handle_incoming_connection(self, sock):
        return sock.accept()

    def service_client(self, client, addr):
        server = Server(client, addr, self.host_key_file)
        server.start()
        return
    
    def _run_server(self, daemon, sock):
        # get host key
        self.host_key_file = os.path.join(
                                get_config('sshproxy').get('hostkey_file'))

        # set up the child killer handler
        signal.signal(signal.SIGCHLD, self.monitor.kill_zombies)
    
    
        try:
            # set up input message queue
            imq = self.imq
            imq[sock] = self
            # set up output message queue
            omq = []
            # set up error message queue
            emq = []

            while True:
                try:
                    # message ready ?
                    try:
                        imr, omr, emr = select.select(imq.keys(), omq, emq, 100)
                    except KeyboardInterrupt:
                        raise
                    except select.error,e:
                        # may be caused by SIGCHLD
                        self.monitor.kill_zombies()
                        continue
                    except socket.error:
                        # may be caused by SIGCHLD
                        self.monitor.kill_zombies()
                        continue

                    for x in imr:
                        if imq[x] is self.monitor:
                            # consume the message
                            self.monitor.handle_incoming_connection(x)
                            continue

                        try:
                            client, addr = imq[x].handle_incoming_connection(x)
                        except socket.error:
                            continue
                        except Exception, e:
                            log.exception('ERROR: socket accept failed')
                            pass
                            raise

                        log.debug('Got a connection!')
                        pid = os.fork()
                        if pid == 0:
                            # just serve in the child
                            x.close()
                            for i in imq:
                                if hasattr(imq[i], 'sock'):
                                    imq[i].sock.close()
                            self.monitor.clean_at_fork()
                            log.info("Serving %s", addr)
                            imq[x].service_client(client, addr)
                            time.sleep(0.5)
                            sys.exit()

                        client.close()
                        self.monitor.add_child(pid, chan=x, ip_addr=addr)
    
    
                except KeyboardInterrupt:
                    log.info("Caught KeyboardInterrupt, exiting...")
                    # don't accept connections anymore
                    sock.close()
                    ### FIX
                    self.imq.pop(0)
                    log.info("Signaling all child processes")
                    msg = ('General shutdown happening.\n'
                           'Please reconnect later.')
                    self.monitor.rq_kill(0, '', '*', msg)
                    # let the clients get their messages
                    if self.monitor.children_count():
                        seconds = 2
                        signal.signal(signal.SIGALRM, self.abort)
#.........这里部分代码省略.........
开发者ID:OutOfOrder,项目名称:sshproxy,代码行数:103,代码来源:daemon.py


注:本文中的monitor.Monitor.children_count方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。