Celery:“function”对象没有属性“apply_async”

问题描述 投票:0回答:1

尝试运行 celery 任务时出现错误

'function' object has no attribute 'apply_async'

db_sinc.py

def create_or_update_google_creative():
    ...logic

任务.py

@shared_task
def run_create_or_update_google_creative():
    print('Task running')
    try:
        result = create_or_update_google_creative()
        return 201, {'message': 'Database successfully updated'}
    except Exception as ex:
        return 500, {'message': 'Database query error'}

views.py

def run_db_sinc():
    result = run_create_or_update_google_creative.apply_async()
    print('+++++++++')
    print(result.get(timeout=1))

当我运行

run_db_sinc
时出现错误。怎么解决?

p/s

根据评论 sahasrara62 更改代码后,功能正在运行,但最终我得到了

tuple' object has no attribute 'apply_async'
gpanel_1  | Traceback (most recent call last):
gpanel_1  |   File "/usr/local/lib/python3.10/site-packages/ninja/operation.py", line 99, in run
gpanel_1  |     result = self.view_func(request, **values)
gpanel_1  |   File "/app/apps/creative_performer/api.py", line 63, in save_creative
gpanel_1  |     return run_db_sinc()
gpanel_1  |   File "/app/apps/creative_performer/views.py", line 136, in run_db_sinc
gpanel_1  |     result = x.apply_async()
gpanel_1  | AttributeError: 'tuple' object has no attribute 'apply_async'

我看不到

的结果
 print('+++++++++')
    print(result.get(timeout=1))
python celery
1个回答
0
投票

你的项目中使用什么版本的celery?

celery 文档中有关“版本 5.0 的删除”的注释

芹菜5:

MyTask.apply_async()          # NO LONGER WORKS


MyTask().apply_async()        # WORKS!
© www.soinside.com 2019 - 2024. All rights reserved.