Sim*_*ran 5 celery python-3.x django-celery
在我的 Django 应用程序中,我使用 celery @task 过滤器定义了一个函数。
# tasks.py
# import statements
@app.task
def categorise():
# LOC
return results # results should be returning json data
Run Code Online (Sandbox Code Playgroud)
我正在使用 Celery 的 .delay() 方法来调用此函数:
def driver(self):
results = categorise.delay()
print("type of results: ", type(results))
return results
Run Code Online (Sandbox Code Playgroud)
results 变量中返回的类型为:
type of results: <class 'celery.result.AsyncResult'>
Run Code Online (Sandbox Code Playgroud)
我遇到以下错误:
Object of type 'AsyncResult' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
在我的 Django settings.py 文件中,我添加了以下配置,但没有帮助:
# REDIS related settings
REDIS_HOST = 'localhost'
REDIS_PORT = '6379'
BROKER_URL = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 3600}
CELERY_RESULT_BACKEND = 'redis://' + REDIS_HOST + ':' + REDIS_PORT + '/0'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
Run Code Online (Sandbox Code Playgroud)
但是我不确定如何将异步结果转换为 json 数据。
找到解决方案。
使用 .get() 检索 json 输出。
def driver(self):
results = categorise.apply_async()
final_results = results.get()
return final_results
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4259 次 |
| 最近记录: |