示例#1
0
class Application(object):
    """The Application represents diesel's main loop--
    the coordinating entity that runs all Services, Loops,
    Client protocol work, etc.
    """

    def __init__(self, logger=None):
        global current_app
        current_app = self
        self.hub = EventHub()
        self._run = False
        if logger is None:
            logger = logmod.Logger()
        self.logger = logger
        self._services = []
        self._loops = []

    def run(self):
        """Start up an Application--blocks until the program ends
        or .halt() is called.
        """
        self._run = True
        logmod.set_current_application(self)
        log.info("Starting diesel application")

        for s in self._services:
            s.bind_and_listen()
            self.hub.register(s.sock, s.accept_new_connection, None)
        for l in self._loops:
            l.iterate()

        self.setup()
        while self._run:
            try:
                self.hub.handle_events()
            except SystemExit:
                log.warn("-- SystemExit raised.. exiting main loop --")
                break
            except KeyboardInterrupt:
                log.warn("-- KeyboardInterrupt raised.. exiting main loop --")
                break
            except Exception, e:
                log.error("-- Unhandled Exception in main loop --")
                log.error(traceback.format_exc())

        log.info("Ending diesel application")
示例#2
0
文件: app.py 项目: timdoug/diesel
class Application(object):
    '''The Application represents diesel's main loop--
    the coordinating entity that runs all Services, Loops,
    Client protocol work, etc.
    '''
    def __init__(self, logger=None, allow_app_replacement=False):
        assert (allow_app_replacement or runtime.current_app is None), "Only one Application instance per program allowed"
        runtime.current_app = self
        self.hub = EventHub()
        self.waits = WaitPool()
        self._run = False
        if logger is None:
            logger = logmod.Logger()
        self.logger = logger
        logmod.set_current_application(self)
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            self.logger.critical("ABORTING: %s" % msg)
            self.halt()
        return bail

    def run(self):
        '''Start up an Application--blocks until the program ends
        or .halt() is called.
        '''
        self._run = True
        log.warn('Starting diesel <%s>'
                % self.hub.describe)

        for s in self._services:
            s.bind_and_listen()
            self.hub.register(s.sock, s.accept_new_connection, None,
                self.global_bail("low-level socket error on bound service"))

        for l in self._loops:
            self.hub.schedule(l.wake)

        self.setup()
        def main():
            while self._run:
                try:
                    self.hub.handle_events()
                except SystemExit:
                    log.warn("-- SystemExit raised.. exiting main loop --")
                    break
                except KeyboardInterrupt:
                    log.warn("-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warn("-- ApplicationEnd raised.. exiting main loop --")
                    break
                except Exception, e:
                    log.error("-- Unhandled Exception rose to main loop --")
                    log.error(traceback.format_exc())

            log.info('Ending diesel application')
            runtime.current_app = None
        self.runhub = greenlet(main)
        self.runhub.switch()
示例#3
0
文件: app.py 项目: dowski/aspen
class Application(object):
    '''The Application represents diesel's main loop--
    the coordinating entity that runs all Services, Loops,
    Client protocol work, etc.
    '''
    def __init__(self, logger=None, allow_app_replacement=False):
        assert (allow_app_replacement or runtime.current_app is None
                ), "Only one Application instance per program allowed"
        runtime.current_app = self
        self.hub = EventHub()
        self.waits = WaitPool()
        self._run = False
        if logger is None:
            logger = logmod.Logger()
        self.logger = logger
        logmod.set_current_application(self)
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            self.logger.critical("ABORTING: %s" % msg)
            self.halt()

        return bail

    def run(self):
        '''Start up an Application--blocks until the program ends
        or .halt() is called.
        '''
        self._run = True
        log.info('Starting diesel application')

        for s in self._services:
            s.bind_and_listen()
            self.hub.register(
                s.sock, s.accept_new_connection, None,
                self.global_bail("low-level socket error on bound service"))

        for l in self._loops:
            self.hub.schedule(l.wake)

        self.setup()

        def main():
            while self._run:
                try:
                    self.hub.handle_events()
                except SystemExit:
                    log.warn("-- SystemExit raised.. exiting main loop --")
                    break
                except KeyboardInterrupt:
                    log.warn(
                        "-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warn("-- ApplicationEnd raised.. exiting main loop --")
                    break
                except Exception, e:
                    log.error("-- Unhandled Exception rose to main loop --")
                    log.error(traceback.format_exc())

            log.info('Ending diesel application')
            runtime.current_app = None

        self.runhub = greenlet(main)
        self.runhub.switch()