【问题标题】:Running multiple instances of Tornado gives ioloop is already running error运行多个 Tornado 实例会导致 ioloop is already running 错误
【发布时间】:2020-07-01 16:47:54
【问题描述】:

这就是我的代码的样子,(已排除一些不相关的细节)

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 is already running 错误。 我觉得这是因为几个正在运行的进程试图访问同一个 Tornado 实例,所以会看到这个错误。 我已尝试阅读 tornado 的文档并在线查看其他资源以尝试解决相同的错误,但找不到任何有用的信息。

谁能帮帮我?

【问题讨论】:

  • 您不能在运行循环中调用run_sync。这就是导致错误的原因。要么在任何地方使用 couroutines 和 yield,要么在任何地方使用回调。通过混合这两种方法,您不必要地使代码复杂化。
  • 虽然我不是多处理时,但在循环中使用 run_sync 是有效的。会试试这个。

标签: python multiprocessing tornado python-multiprocessing ioloop


【解决方案1】:

这在我改变后有效 from multiprocessing.pool import ThreadPool as Poolfrom multiprocessing import Pool

我曾经使用 Threadpool 作为解决 Can't pickle <type 'instancemethod'> when using multiprocessing Pool.map() 这个错误的解决方法,但继续使用此处定义的 copy_reg https://laszukdawid.com/2017/12/13/multiprocessing-in-python-all-about-pickling/ 来解决整个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    相关资源
    最近更新 更多