龙卷风中的GeneratorExit问题

时间:2016-06-24 20:34:24

标签: python-3.x tornado psycopg2 yield coroutine

我目前收到此错误。我很困惑,因为我可以告诉发生器退出只是在生成器完成时调用,但我有很多其他生成器继承此类,不会调用此错误。我是否正确设置发电机?或者是否有一些隐含的代码我没有考虑调用close()?

"error": "Traceback (most recent call last):\n  File \"/stashboard/source/stashboard/checkers.py\", line 29, in run\n    yield self.check()\nGeneratorExit\n",

调用此yield语句的代码:

class Checker():

    def __init__(self,  event, frequency, params):
        self.event = event
        self.frequency = frequency
        self.params = params

    @gen.coroutine
    def run(self):
        """ Run check method every <frequency> seconds
        """
        while True:
            try:
                yield self.check()
            except GeneratorExit:
                logging.info("EXCEPTION")
                raise GeneratorExit
            except:
                data = {
                    'status': events.STATUS_ERROR,
                    'error': traceback.format_exc()
                }
                yield self.save(data)
            yield gen.sleep(self.frequency)

    @gen.coroutine
    def check(self):
        pass

    @gen.coroutine
    def save(self, data):
        yield events.save(self.event, data)

这是从中继承的代码:

class PostgreChecker(Checker):
    # checks list of Post
    formatter = 'stashboard.formatters.PostgreFormatter'

    def __init__(self, event, frequency, params):
        super().__init__(event, frequency, params)
        self.clients = []
        for DB in configuration["postgre"]:
            # setup and create connections to PG servers.
            postgreUri = queries.uri(DB["host"], DB["port"], DB["dbName"],
                                     DB["userName"], DB["password"])
            # creates actual link to DB
            client = queries.TornadoSession(postgreUri)
            # starts connection
            client.host = DB["host"]
            self.clients.append(client)

    @gen.coroutine
    def check(self):

        for client in self.clients:
            try:
                yield client.validate()
                self.save({'host': client.host,
                               'status': events.STATUS_OK})
            except (ConnectionError, AutoReconnect, ConnectionFailure):
                self.save({'host': client.host,
                           'status': events.STATUS_FAIL})

2 个答案:

答案 0 :(得分:0)

Tornado永远不会在您的生成器上调用close(),但垃圾收集器会这样做(我认为从Python 3.4开始)。如何调用checker.run()?使用IOLoop.spawn_callback()进行即发即弃协同程序;这将保留对它们的引用并允许它们无限期地继续运行。

答案 1 :(得分:0)

这里的具体问题是我的db游标没有自动重新连接。我使用的是queries库,但切换到momoko并且问题已经消失