Esempio n. 1
0
 def run_subscribe(self):
     e = Observer()
     getcurrent().in_another_greenlet = self.in_another_greenlet
     getcurrent().in_another_greenlet2 = self.in_another_greenlet2
     b = e.subscribe('kill',getcurrent().in_another_greenlet)
     c = e.subscribe('kill',getcurrent().in_another_greenlet2)
     gevent.sleep(1)
     b.unsubscribe()
     c.unsubscribe()
Esempio n. 2
0
class DaemonChild(object):
    """Main runner. Initialize all modules and start process loop"""
    def __init__(self,pnum,sockets=None):
        self.proc_num   = pnum
        self.sockets    = sockets
        self.events     = Observer()
        self.conf       = Config.getInstance()
        
    def run(self):
        logging.debug(self.proc_num)
        self._main_loop()
        """if self.proc_num == 0:    
            self._main_loop()
        elif self.proc_num == 1:
            self._broker_loop()
        elif self.proc_num > 1:
            self._worker_loop()"""
        
        
    @classmethod
    def reload_config(cls):
        pass
       
    def _shutdown(self):
        logging.debug('Shuting down the daemon child.')
        logging.shutdown()
        # Use only for child processes after fork
        os._exit(0)
        
    def _broker_loop(self):
        self.events.subscribe('shutdown',self._shutdown)
        try:
            context = zmq.Context()
            frontend = context.socket(zmq.XREP)
            backend = context.socket(zmq.XREQ)
            frontend.bind("tcp://*:5559")
            backend.bind("tcp://*:5560")

            # Initialize poll set
            poller = zmq.Poller()
            poller.register(frontend, zmq.POLLIN)
            poller.register(backend, zmq.POLLIN)

            # Switch messages between sockets
            while True:
                socks = dict(poller.poll())

                if socks.get(frontend) == zmq.POLLIN:
                    message = frontend.recv()
                    more = frontend.getsockopt(zmq.RCVMORE)
                    if more:
                        backend.send(message, zmq.SNDMORE)
                    else:
                        backend.send(message)

                if socks.get(backend) == zmq.POLLIN:
                    message = backend.recv()
                    more = backend.getsockopt(zmq.RCVMORE)
                    if more:
                        frontend.send(message, zmq.SNDMORE)
                    else:
                        frontend.send(message)

        except Exception,e:
            logging.exception(e)
            self._shutdown()