celery consume send_task response

流过昼夜 提交于 2021-02-11 15:19:10

问题


In django application I need to call an external rabbitmq, running on a windows server and using some application there, where the django app runs on a linux server.

I'm currently able to add a task to the queue by using the celery send_task:

app.send_task('tasks', kwargs=self.get_input(), queue=Queue('queue_async', durable=False))

My settings looks like:

CELERY_BROKER_URL = CELERY_CONFIG['broker_url']
BROKER_TRANSPORT_OPTIONS = {"max_retries": 3, "interval_start": 0, "interval_step": 0.2, "interval_max": 0.5}

CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_DEFAULT_QUEUE = 'celery'
CELERY_TASK_RESULT_EXPIRES = 3600
CELERY_RESULT_BACKEND = 'rpc://'
CELERY_CREATE_MISSING_QUEUES = True

What I'm not sure about is how I can get and parse the response, since the send_task only returns a key?


回答1:


If you want to store results of your task , you could use this parameter result_backend or CELERY_RESULT_BACKEND depending on the version of celery you're using.

Complete list of Configuration options can be found here (search for result_backend on this page) => https://docs.celeryproject.org/en/stable/userguide/configuration.html

Many options are available to store results - SQL DBs , NoSQL DBs, Elasticsearch, Memcache, Redis, etc,etc . Choose as per your project stack.




回答2:


Thanks that helped for the understanding. So since I want to further process the answer I use rpc as already defined in the config I had in the example.

What I found usefull was this example, because most python celery examples assume that the consumer is the same application, that describes the interaction to a Java app Celery-Java since it gives a good example on how to request from python side.

Therefore my implementation is now:

result = app.signature('tasks', kwargs=self.get_input(), queue=Queue('queue_async', durable=False)).delay().get()

which waits and parses the result.



来源:https://stackoverflow.com/questions/62383513/celery-consume-send-task-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!