我在tornado.ioloop worker中使用了celery,因为我需要使用mongodb。

class WorkerBase():
    @gen.engine
    def foo(self,args,callback)
        bar = ['Python','Celery','Javascript','HTML']

        # ... process something ....

        callback(bar)

    @gen.engine
    def RunMyTask(self,args):

        result = yield gen.Task(self.foo,args=args)
        # Stop IOLoop instance
        IOLoop.instance().stop()


@task(name="MyWorker",base=WorkerBase)
def CeleryWorker(args):
    # This works because i'm adding base as WorkerBase
    CeleryWorker.RunMyTask(args)
    IOLoop.instance().start()
    return True


当我调用任务时,出现错误提示:

[2014-10-02 12:12:11,561: ERROR/Worker-4] Exception in callback None
Traceback (most recent call last):
    File "/var/www/myapp/env/local/lib/python2.7/site-packages/tornado/ioloop.py", line 832, in start
fd_obj, handler_func = self._handlers[fd]
KeyError: 16


要么

[2014-10-02 12:12:11,561: ERROR/Worker-4] Exception in callback None
Traceback (most recent call last):
    File "/var/www/myapp/env/local/lib/python2.7/site-packages/tornado/ioloop.py", line 832, in start
fd_obj, handler_func = self._handlers[fd]
KeyError: 14


这些错误不一致。有加薪条件吗?

最佳答案

这看起来像一个线程问题。我对celery的线程模型不熟悉,但似乎正在启动CeleryWorker的多个副本,每个副本都试图运行相同的单例IOLoop.instance()。如果您要像这样运行每个工作线程,则每个工作线程都需要自己的IOLoop-查看同步tornado.httpclient.HTTPClient创建和运行临时IOLoop的操作

关于python - celery worker 中回调中的Tornado IOLoop异常无,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26174676/

10-12 21:54