我正在尝试为我的项目设置 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 文件中,我设置了亚马逊凭据,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 应用程序中添加了包含以下代码的 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'
当我在预测/预测文件夹中运行此命令时,它会回溯许多错误,其中一些错误 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
我应该怎么做才能解决这个问题
这可能无法解决整个问题,但
from .celery import app as
celery_app__all__ = ['celery_app']
应改为
from .celery import app as celery_app
__all__ = ('celery_app',)
我认为这将帮助像我这样的新手:
您应该为工作人员提供完整的项目名称:celery worker --app config.celery.app
这是我在django中的观察。项目文件夹结构为
abc/
config/
celery.py
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
- app = Celery("test")
settings.py
如果遵循与文档和任务应用程序命名约定中指定的相同文件夹结构,则可以将应用程序名称(假设测试(提供给辅助角色celery worker --app test
abc/
test/
celery.py
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")
- app = Celery("test")
settings.py
然后芹菜自动检测测试.芹菜.应用
我也有类似的问题。您只需要检查您是否在正确的目录中。
如果您位于predictions
目录中,请运行cd forecast
。
这应该很好,因为芹菜试图在不存在的predictions/forecast/
中查找芹菜应用程序设置。你的芹菜应用程序在predictions/forecast/forecast/
,也就是说,在你的 django 项目目录中。
在init中.py您的预测目录添加以下内容并尝试重新启动这可能会起作用:
from .celery import app as celery_app
__all__ = ('celery_app',)