我在两台不同的机器上运行芹菜客户端(Flask)和工人,现在一旦工人完成任务,我需要在客户端回调一个函数。这可能吗?
芹菜客户:-
celery_app=Celery('test_multihost', broker='amqp://test:test@<worker_ip>/test_host', backend='rpc')
result= testMethod1.apply_async((param1, param2,param3), link=testMethod2.s())
@celery_app.task
def testMethod2():
#testMethod2 body.
芹菜工人:-
celery_app=Celery('test_multihost', broker='amqp://test:test@<worker_ip>/test_host', backend='rpc')
@celery_app.task
def testMethod1():
#testMethod1 body
但问题是函数 testMethod2 是在 celery worker 端执行的,而不是在客户端。
无论如何,我可以在客户端回调该方法吗?
我使用 celery 信号中的 @after_task_publish 解决了这个问题。代码片段如下:-
@after_task_publish.connect(sender=<registered_celery_task>)
def testMethod2(sender=None, headers=None, body=None, **kwargs):
#callback body
testMethod2 将在远程机器上的芹菜工作者完成后被调用。在这里,我可以使用headers参数访问 celery worker 的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句