@defer.inlineCallbacks
    def run_as_leader(self):
        logger.info('Running as leader for service [{0}]. pid: [{1}]'.format(self.service_name, os.getpid()))

        try:
            notification_queue = yield self.cancellable(self.listener.listen(self.channels))

            yield self.cancellable(self.process_initial())

            while self.running:
                event = yield self.cancellable(notification_queue.get())

                handler = self.get_handler(event.channel)
                if not handler:
                    logger.warn('no handler for event [{0}]'.format(event.channel))
                    continue

                try:
                    payload = self.get_payload(event)
                except (ValueError, __HOLE__):
                    continue

                try:
                    result = yield handler(payload)
                except defer.CancelledError:
                    raise
                except Exception as e:
                    logger.exception('unhandled exception in run_as_leader')

        except defer.CancelledError:
            pass

        finally:
            self.listener.unlisten(notification_queue)
dataset/ETHPy150Open foundit/Piped/contrib/database/piped_database/service.py/PostgresListenerService.run_as_leader