从龙卷风调用芹菜任务

时间:2011-10-28 20:40:16

标签: python celery tornado

有人如何从龙卷风中调用芹菜任务,并通过回调获得结果?

这个post声称有人必须简单地通过RabbitMQ发送消息然后执行该任务。这是有道理的,但是有人可以在python中给出一个例子(龙卷风更好,有回调)吗?就个人而言,我使用mongodb作为我的消息代理,但我也可以切换到Redis或RabbitMQ ..

编辑:为了澄清事情,我想要一个带回调的例子。例如,这个龙卷风代码

TestTask.delay(callback = self._on_celery_response) 
...
def _on_celery_response(self, result):
    print "hello from _on_celery_repsonse" , result

不起作用。我的TestTask是:

class TestTask(Task):
    name = "tornadoServer.Test"
    def run(self, callback=None,  **kwargs):
        result = {'result': "hello from celery task invoked by tornado"}
        if callback is not None:
            subtask(callback).delay(result)
        return result

和追溯:

    File "/home/hymloth/Desktop/DJANGO/NOO1/tornadoServer/tornado/stack_context.py", line 183, in wrapped
    callback(*args, **kwargs)
  File "/home/hymloth/Desktop/DJANGO/NOO1/tornadoServer/asyncmongo/connection.py", line 183, in _parse_response
    callback(response)
  File "/home/hymloth/Desktop/DJANGO/NOO1/tornadoServer/asyncmongo/cursor.py", line 399, in _handle_response
    orig_callback(result['data'], error=None)
  File "/home/hymloth/Desktop/DJANGO/NOO1/tornadoServer/basic_auth_handlers.py", line 66, in _on_response
    celery_tasks.TestTask.delay(self._on_celery_response)
  File "/usr/local/lib/python2.6/dist-packages/celery-2.2.7-py2.6.egg/celery/task/base.py", line 338, in delay
    return self.apply_async(args, kwargs)
  File "/usr/local/lib/python2.6/dist-packages/celery-2.2.7-py2.6.egg/celery/task/base.py", line 460, in apply_async
    **options)
  File "/usr/local/lib/python2.6/dist-packages/celery-2.2.7-py2.6.egg/celery/app/amqp.py", line 230, in delay_task
    send(body, exchange=exchange, **extract_msg_options(kwargs))
  File "/usr/local/lib/python2.6/dist-packages/kombu-1.1.6-py2.6.egg/kombu/compat.py", line 101, in send
    return self.publish(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py", line 124, in publish
    compression, headers)
  File "/usr/local/lib/python2.6/dist-packages/kombu-1.1.6-py2.6.egg/kombu/messaging.py", line 147, in _prepare
    body) = encode(body, serializer=serializer)
  File "/usr/local/lib/python2.6/dist-packages/kombu-1.1.6-py2.6.egg/kombu/serialization.py", line 119, in encode
    payload = encoder(data)
  File "/usr/lib/python2.6/copy_reg.py", line 70, in _reduce_ex
    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects

没有回调,任务正常工作..有什么建议吗?

3 个答案:

答案 0 :(得分:0)

回调对象也应该是芹菜任务,否则你的代码不起作用。

如果您的回调函数不必是芹菜任务,您可以在任务正文中使用信号。

http://docs.python.org/library/signal.html

答案 1 :(得分:-1)

如果你的龙卷风过程的pythonpath中的任务模块很简单:

my_task.delay()
只要在龙卷风实例中正确配置芹菜,

就可以正常工作。要在启动服务器时执行此操作:

CELERY_CONFIG_MODULE=app.foo.celeryconfig

在环境中,所以当你导入芹菜时,芹菜设置会知道你的实例设置在哪里。

答案 2 :(得分:-1)

芹菜有回调功能,你可以去http://ask.github.com/celery/userguide/tasksets.html

from celery.task import task
from celery.task.sets import subtask

@task
def add(x, y, callback=None):
    result = x + y

    if callback is not None:
        subtask(callback).delay(result)

    return result