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
 def run_cancel(self):
     try:
         e = Observer()
         ev = e.wait('kill')
         ev.cancel()
         gevent.sleep(1)
     except FiredEvent:
         return False
     else:
         return True
Esempio n. 3
0
 def run_wait(self):
     e = Observer()
     ev = e.wait('kill')
     try:
         gevent.sleep(1)
     except FiredEvent:
         return True
     else:
         return False
     finally:
         ev.cancel()
Esempio n. 4
0
File: app.py Progetto: creotiv/gflib
 def _reload_config(self):
     o = Observer()
     o.fire('reload_config')
Esempio n. 5
0
File: app.py Progetto: creotiv/gflib
 def _stop_child(self, sig=None, frame=None):
     """It's executed when server shutdown. Used for correct shutdown.
     """     
     e = Observer()
     e.fire('shutdown')
Esempio n. 6
0
 def fire_event(self):
     e2 = Observer()
     e2.fire('kill')
Esempio n. 7
0
 def __init__(self,pnum,sockets=None):
     self.proc_num   = pnum
     self.sockets    = sockets
     self.events     = Observer()
     self.conf       = Config.getInstance()
Esempio n. 8
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()