运行多个Tornado实例时出现ioloop已经运行错误。

3
这是我的代码样式,(已省略一些不太相关的细节)。
from multiprocessing.pool import ThreadPool as Pool

class GetUsers(BaseTask):
    def foo(self):
        pool = Pool()
        try:
            pool.map(self.bar, users)
        finally:
            pool.close()
            pool.join()

    def bar(self, users):
        uuid = users[0]
        ioloopInstance = ioloop.IOLoop().instance()
        isInExperiment = self.isInExperiment(uuid, ioloopInstance)
        log.info(str(uuid)+str(isInExperiment))

    def isInExperiment(self, uuid, ioloop):
        isInExpTag_response =ioloop.run_sync(lambda: self.
                                             fetch_isInExperiment_response(uuid))
        if len(isInExpTag_response.body) > 0:
            return True
        return False

    @gen.coroutine
    def fetch_isInExperiment_response(self, uuid):
        response = yield baz
        raise gen.Return(response)


当我运行它时,出现“ioloop已在运行”的错误。我认为这是因为运行的多个进程正在尝试访问Tornado的同一实例,因此会看到此错误。我尝试阅读了Tornado文档并查看了其他在线资源以尝试解决相同的错误,但找不到任何有用的信息。能否有人帮助我解决问题?

你不能在一个正在运行的循环中调用 run_sync。这就是导致错误的原因。要么在所有地方使用协程和 yield,要么在所有地方使用回调函数。通过混合这两种方法,你会不必要地使代码变得复杂。 - xyres
在未使用多进程的情况下,在循环中使用run_sync起作用了。我会尝试这种方法。 - Swastik Udupa
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接