如何解决这个 celery 错误:无法加载 celery 应用程序。模块“预测”没有属性“芹菜”

Posted

技术标签:

【中文标题】如何解决这个 celery 错误:无法加载 celery 应用程序。模块“预测”没有属性“芹菜”【英文标题】:How to solve this celery Error: Unable to load celery application. Module 'forecast' has no attribute 'celery' 【发布时间】:2020-01-25 18:19:57 【问题描述】:

我正在尝试为我的项目设置 Celery 和 Amazon SQS,但遇到了问题。我做了以下。我将 celery.py 文件放在设置 py 所在的项目目录中。这是我的项目的快照

--predictions
     --forecast
        --dataflow
        --forecast
              **  __init__.py
              **  celery.py
              **  urls.py
               ** settings.py
               ** wsgi.py
        --manage.py
     --env

我喜欢“--”文件夹和“**”文件。在 celery.py 文件中,我有以下代码

from __future__ import      
absolute_import, unicode_literals
import os
from celery import Celery                       


os.environ.setdefault
('DJANGO_SETTINGS_MODULE',
'forecast.settings')

app = Celery('forecast')
app.config_from_object
('django.conf:settings')

app.autodiscover_tasks()

app.conf.beat_schedule = 
'display_time-30-seconds':   
 'task':  'demoapp.tasks.display_time',
    'schedule': 10.0
 ,
                                                 
@app.task(bind=True)                             
def debug_task(self):
    print('Request:     
    0r'.format(self.request))

在 settings.py 文件中我设置了 Amazon 凭证、broker_url 等

#settings.py
# AWS Credentials                                 
AWS_ACCESS_KEY_ID = ('lol')      
AWS_SECRET_ACCESS_KEY =    
('Lol')                                                                              

# Celery                                          
BROKER_URL = "sqs://%s:%s@" %   
(AWS_ACCESS_KEY_ID, 
AWS_SECRET_ACCESS_KEY)                        
CELERY_ACCEPT_CONTENT = 
['application/json']    
CELERY_RESULT_SERIALIZER = 'json'               
CELERY_TASK_SERIALIZER = 'json'                 
CELERY_DEFAULT_QUEUE = 'test.fifo'              
CELERY_RESULT_BACKEND = None # 
Disabling the results backend

BROKER_TRANSPORT_OPTIONS =    
'region': 'us-west-2',                          
'polling_interval': 20,

init.py 文件中我放了以下代码

from __future__ import   
absolute_import, unicode_literals
from .celery import app as 
celery_app__all__ = ['celery_app']

然后我在我的 django 应用 task.py 中添加了包含以下代码的代码

from datetime import datetime                   
from celery import shared_task                  
@shared_task
def display_time(x, y):                                
     print("The time is %s :" % 
     str(datetime.now()))                                                 
     return True

之后我在 AWS 默认队列中创建。比终端中的预测文件夹内我运行以下命令

celery -A forecast worker -l info

这给了我错误

Error: Unable to load celery 
application. Module 'forecast' has no 
attribute 'celery'

当我在 predictions/forecast 文件夹中运行此命令时,它会回溯许多错误,其中一些错误在 models.py 文件中。这是回溯

Traceback (most recent call last):
File "/data/data/com.termux/files/
home/storage/predictions/env/bin/
celery", line 10, in <module>
sys.exit(main())
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/   
python3.7/site-packages/celery/ 
__main__.py", line 16, in main
_main()
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 322, in main
cmd.execute_from_commandline
(argv)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 496, in 
execute_from_commandline
super(CeleryCommand, self)
.execute_from_commandline(argv)))
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/ 
base.py", line 298, in 
execute_from_commandline
return 
self.handle_argv(self.prog_name, 
argv[1:])
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 488, in handle_argv
return self.execute(command, argv)
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 420, in execute
).run_from_argv(self.prog_name, 
argv[1:], command=argv[0])
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
return 
self.handle_argv(self.prog_name,   
argv[1:])
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 488, in handle_argv
return self.execute(command, argv)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
celery.py", line 420, in execute
).run_from_argv(self.prog_name,    
argv[1:], command=argv[0])
File "/data/data/com.termux/files/ 
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 257, in run
**kwargs)
File "/data/data/com.termux/files/   
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 223, in run_from_argv
return self(*args, **options)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
base.py", line 252, in __call__
ret = self.run(*args, **kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/bin/
worker.py", line 257, in run
**kwargs)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
worker/worker.py", line 96, in __init__
self.app.loader.init_worker()
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
loaders/base.py", line 114, in  
init_worker
self.import_default_modules()
File "/data/data/com.termux/files/  
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
loaders/base.py", line 108, in 
import_default_modules
File "/data/data/com.termux/files/     
home/storage/predictions/env/lib/
python3.7/site-packages/celery/utils/
dispatch/signal.py", line 288, in send
response = receiver(signal=self,    
sender=sender, **named)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 84, in 
on_import_modules
self.worker_fixup.validate_models()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 122, in 
validate_models
self.django_setup()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/celery/
fixups/django.py", line 118, in    
django_setup
django.setup()
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/ 
__init__.py", line 24, in setup  
apps.populate
(settings.INSTALLED_APPS)
File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/   
apps/registry.py", line 114, in populate
app_config.import_models()

File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/site-packages/django/
apps/config.py", line 211, in 
import_models
self.models_module=import_module
(models_module_name)

File "/data/data/com.termux/files/
home/storage/predictions/env/lib/
python3.7/importlib/__init__.py", line 
127, in import_module  return 
_bootstrap._gcd_import(name[level:], 
package, level)
File "<frozen importlib._bootstrap>", 
line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", 
line 983, in _find_and_load

File "<frozen importlib._bootstrap>", 
line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", 
line 677, in _load_unlocked
File "<frozen 
importlib._bootstrap_external>", line 
724, in exec_module
File "<frozen 
importlib._bootstrap_external>", line 
860, in get_code
File "<frozen 
importlib._bootstrap_external
File "<frozen 
importlib._bootstrap_external>", line 
860, in get_code
File "<frozen 
importlib._bootstrap_external>", line 
791, in source_to_code
File "<frozen importlib._bootstrap>", 
line 219, in 
_call_with_frames_removed
File "/data/data/com.termux/files/ 
home/storage/predictions/forecast/
dataflow/models.py", line 78
 home_startXI = models.JSONField()               
 home_substitutes = 
 models.JSONField()           
 away_formation = 
 models.CharField(null=True)    
 away_startXI = models.JSONField()               
 away_substitutes = 
 models.JSONField()
                                                               ^
 SyntaxError: invalid syntax

我应该怎么做才能解决这个问题

【问题讨论】:

【参考方案1】:

我遇到了类似的问题。您只需要检查您是否在正确的目录中。

如果您在 predictions 目录中,请运行 cd forecast

这应该很好,因为 celery 会尝试在 predictions/forecast/ 中查找不存在的 celery 应用程序设置。您的 celery 应用位于predictions/forecast/forecast/,即在您的 django 项目目录中。

【讨论】:

【参考方案2】:

在您的预测目录的 init.py 中添加以下内容并尝试重新启动这可能会起作用:

    from .celery import app as celery_app
    __all__ = ('celery_app',)

【讨论】:

【参考方案3】:

我认为这将有助于像我这样的新手:

您应该将完整的项目名称提供给工作人员: celery worker --app config.celery.app

这是我在 django.Project 中观察到的文件夹结构是

abc/
   config/
     celery.py
           - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
           - app = Celery("test")
     settings.py

如果您遵循 doc 和任务应用程序命名约定中指定的相同文件夹结构,那么您可以将应用程序名称(比如说测试)提供给工作人员 celery worker --app test

abc/
  test/
     celery.py
           - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")
           - app = Celery("test")
     settings.py

然后 celery 自动检测 test.celery.applcation

【讨论】:

【参考方案4】:

这可能无法解决整个问题,但

from .celery import app as 
celery_app__all__ = ['celery_app']

应该阅读

from .celery import app as celery_app
__all__ = ('celery_app',)

【讨论】:

以上是关于如何解决这个 celery 错误:无法加载 celery 应用程序。模块“预测”没有属性“芹菜”的主要内容,如果未能解决你的问题,请参考以下文章

Celery sqs 尝试连接到 amqp:无法连接到 amqp://guest:**@127.0.0.1:5672//

celery

Flask + Celery + Redis:消费者:无法连接到 amqp://guest:**@127.0.0.1:5672//:超时

如何在 django celery 中处理未完成的任务

Celery 分布式任务队列快速入门

如何解决Eclipse下“找不到或无法加载主类”的错误