示例#1
0
文件: app.py 项目: arnaudsj/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, 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
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            log.critical("ABORTING: {0}", 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.warning('Starting diesel <{0}>', self.hub.describe)

        for s in self._services:
            s.bind_and_listen()
            s.register(self)

        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.warning("-- SystemExit raised.. exiting main loop --")
                    raise
                except KeyboardInterrupt:
                    log.warning("-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warning("-- 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()
示例#2
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, 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
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            log.critical("ABORTING: {0}", 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.warning('Starting diesel <{0}>', self.hub.describe)

        for s in self._services:
            s.bind_and_listen()
            s.register(self)

        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.warning("-- SystemExit raised.. exiting main loop --")
                    raise
                except KeyboardInterrupt:
                    log.warning(
                        "-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warning(
                        "-- 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 项目: 1angxi/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, 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
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            log.critical("ABORTING: {0}", msg)
            self.halt()
        return bail

    def run(self):
        '''Start up an Application--blocks until the program ends
        or .halt() is called.
        '''
        profile = os.environ.get('DIESEL_PROFILE', '').lower() in YES_PROFILE
        track_gc = os.environ.get('TRACK_GC', '').lower() in YES_PROFILE
        track_gc_leaks = os.environ.get('TRACK_GC_LEAKS', '').lower() in YES_PROFILE
        if track_gc:
            gc.set_debug(gc.DEBUG_STATS)
        if track_gc_leaks:
            gc.set_debug(gc.DEBUG_LEAK)

        self._run = True
        log.warning('Starting diesel <{0}>', self.hub.describe)

        for s in self._services:
            s.bind_and_listen()
            s.register(self)

        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.warning("-- SystemExit raised.. exiting main loop --")
                    raise
                except KeyboardInterrupt:
                    log.warning("-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warning("-- 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

        def _profiled_main():
            log.warning("(Profiling with cProfile)")

            # NOTE: Scoping Issue:
            # Have to rebind _main to _real_main so it shows up in locals().
            _real_main = _main
            config = {'sort':1}
            statsfile = os.environ.get('DIESEL_PSTATS', None)
            if statsfile:
                config['filename'] = statsfile
            try:
                cProfile.runctx('_real_main()', globals(), locals(), **config)
            except TypeError, e:
                if "sort" in e.args[0]:
                    del config['sort']
                    cProfile.runctx('_real_main()', globals(), locals(), **config)
                else: raise e
示例#4
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, 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
        self._services = []
        self._loops = []

        self.running = set()

    def global_bail(self, msg):
        def bail():
            log.critical("ABORTING: {0}", msg)
            self.halt()

        return bail

    def run(self):
        '''Start up an Application--blocks until the program ends
        or .halt() is called.
        '''
        profile = os.environ.get('DIESEL_PROFILE', '').lower() in YES_PROFILE
        track_gc = os.environ.get('TRACK_GC', '').lower() in YES_PROFILE
        track_gc_leaks = os.environ.get('TRACK_GC_LEAKS',
                                        '').lower() in YES_PROFILE
        if track_gc:
            gc.set_debug(gc.DEBUG_STATS)
        if track_gc_leaks:
            gc.set_debug(gc.DEBUG_LEAK)

        self._run = True
        log.warning('Starting diesel <{0}>', self.hub.describe)

        for s in self._services:
            s.bind_and_listen()
            s.register(self)

        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.warning("-- SystemExit raised.. exiting main loop --")
                    raise
                except KeyboardInterrupt:
                    log.warning(
                        "-- KeyboardInterrupt raised.. exiting main loop --")
                    break
                except ApplicationEnd:
                    log.warning(
                        "-- 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

        def _profiled_main():
            log.warning("(Profiling with cProfile)")

            # NOTE: Scoping Issue:
            # Have to rebind _main to _real_main so it shows up in locals().
            _real_main = _main
            config = {'sort': 1}
            statsfile = os.environ.get('DIESEL_PSTATS', None)
            if statsfile:
                config['filename'] = statsfile
            try:
                cProfile.runctx('_real_main()', globals(), locals(), **config)
            except TypeError, e:
                if "sort" in e.args[0]:
                    del config['sort']
                    cProfile.runctx('_real_main()', globals(), locals(),
                                    **config)
                else:
                    raise e