Наг*_*мер 5 python django django-models celery django-celery
我正在尝试为我的项目设置 Celery 和 Amazon SQS\xc2\xa0 但遇到问题。我做了以下。我将 celery.py 文件放在设置 py 所在的项目目录中。这是我的项目\xc2\xa0 的快照
\n\n--predictions\n --forecast\n --dataflow\n --forecast\n ** __init__.py\n ** celery.py\n ** urls.py\n ** settings.py\n ** wsgi.py\n --manage.py\n --env\nRun Code Online (Sandbox Code Playgroud)\n\n我很喜欢“--”文件夹和“**”文件。在 celery.py 文件内我有以下代码
\n\nfrom __future__ import \nabsolute_import, unicode_literals\nimport os\nfrom celery import Celery \n\n\nos.environ.setdefault\n(\'DJANGO_SETTINGS_MODULE\',\n\'forecast.settings\')\n\napp = Celery(\'forecast\')\napp.config_from_object\n(\'django.conf:settings\')\n\napp.autodiscover_tasks()\n\napp.conf.beat_schedule = {\n\'display_time-30-seconds\': \n{ \'task\': \'demoapp.tasks.display_time\',\n \'schedule\': 10.0\n },\n} \n@app.task(bind=True) \ndef debug_task(self):\n print(\'Request: \n {0r}\'.format(self.request))\nRun Code Online (Sandbox Code Playgroud)\n\n在 settings.py 文件中我设置了 Amazon 凭证、broker_url 等
\n\n#settings.py\n# AWS Credentials \nAWS_ACCESS_KEY_ID = (\'lol\') \nAWS_SECRET_ACCESS_KEY = \n(\'Lol\') \n\n# Celery \nBROKER_URL = "sqs://%s:%s@" % \n(AWS_ACCESS_KEY_ID, \nAWS_SECRET_ACCESS_KEY) \nCELERY_ACCEPT_CONTENT = \n[\'application/json\'] \nCELERY_RESULT_SERIALIZER = \'json\' \nCELERY_TASK_SERIALIZER = \'json\' \nCELERY_DEFAULT_QUEUE = \'test.fifo\' \nCELERY_RESULT_BACKEND = None # \nDisabling the results backend\n\nBROKER_TRANSPORT_OPTIONS = \n{\'region\': \'us-west-2\', \n\'polling_interval\': 20,}\nRun Code Online (Sandbox Code Playgroud)\n\n在init .py 文件中我放置了以下代码
\n\nfrom __future__ import \nabsolute_import, unicode_literals\nfrom .celery import app as \ncelery_app__all__ = [\'celery_app\']\nRun Code Online (Sandbox Code Playgroud)\n\n然后我添加到我的 django 应用程序 task.py 中,其中包含以下代码
\n\nfrom datetime import datetime \nfrom celery import shared_task \n@shared_task\ndef display_time(x, y): \n print("The time is %s :" % \n str(datetime.now())) \n return True\nRun Code Online (Sandbox Code Playgroud)\n\n之后我在AWS默认队列中创建。比终端中的预测文件夹内我运行以下命令
\n\ncelery -A forecast worker -l info\nRun Code Online (Sandbox Code Playgroud)\n\n这给了我错误
\n\nError: Unable to load celery \napplication. Module \'forecast\' has no \nattribute \'celery\'\nRun Code Online (Sandbox Code Playgroud)\n\n当我在 Predictions/forecast 文件夹中运行此命令时,它会回溯许多错误,其中一些错误位于 models.py 文件中。这是回溯
\n\nTraceback (most recent call last):\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/bin/\ncelery", line 10, in <module>\nsys.exit(main())\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/ \npython3.7/site-packages/celery/ \n__main__.py", line 16, in main\n_main()\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 322, in main\ncmd.execute_from_commandline\n(argv)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 496, in \nexecute_from_commandline\nsuper(CeleryCommand, self)\n.execute_from_commandline(argv)))\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/ \nbase.py", line 298, in \nexecute_from_commandline\nreturn \nself.handle_argv(self.prog_name, \nargv[1:])\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 488, in handle_argv\nreturn self.execute(command, argv)\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 420, in execute\n).run_from_argv(self.prog_name, \nargv[1:], command=argv[0])\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nworker.py", line 223, in run_from_argv\nreturn self(*args, **options)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nbase.py", line 252, in __call__\nret = self.run(*args, **kwargs)\nreturn \nself.handle_argv(self.prog_name, \nargv[1:])\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 488, in handle_argv\nreturn self.execute(command, argv)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\ncelery.py", line 420, in execute\n).run_from_argv(self.prog_name, \nargv[1:], command=argv[0])\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nworker.py", line 223, in run_from_argv\nreturn self(*args, **options)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nbase.py", line 252, in __call__\nret = self.run(*args, **kwargs)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nworker.py", line 257, in run\n**kwargs)\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nworker.py", line 223, in run_from_argv\nreturn self(*args, **options)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nbase.py", line 252, in __call__\nret = self.run(*args, **kwargs)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/bin/\nworker.py", line 257, in run\n**kwargs)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nworker/worker.py", line 96, in __init__\nself.app.loader.init_worker()\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nloaders/base.py", line 114, in \ninit_worker\nself.import_default_modules()\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nloaders/base.py", line 108, in \nimport_default_modules\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/utils/\ndispatch/signal.py", line 288, in send\nresponse = receiver(signal=self, \nsender=sender, **named)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nfixups/django.py", line 84, in \non_import_modules\nself.worker_fixup.validate_models()\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nfixups/django.py", line 122, in \nvalidate_models\nself.django_setup()\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/celery/\nfixups/django.py", line 118, in \ndjango_setup\ndjango.setup()\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/django/ \n__init__.py", line 24, in setup \napps.populate\n(settings.INSTALLED_APPS)\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/django/ \napps/registry.py", line 114, in populate\napp_config.import_models()\n\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/site-packages/django/\napps/config.py", line 211, in \nimport_models\nself.models_module=import_module\n(models_module_name)\n\nFile "/data/data/com.termux/files/\nhome/storage/predictions/env/lib/\npython3.7/importlib/__init__.py", line \n127, in import_module return \n_bootstrap._gcd_import(name[level:], \npackage, level)\nFile "<frozen importlib._bootstrap>", \nline 1006, in _gcd_import\nFile "<frozen importlib._bootstrap>", \nline 983, in _find_and_load\n\nFile "<frozen importlib._bootstrap>", \nline 967, in _find_and_load_unlocked\nFile "<frozen importlib._bootstrap>", \nline 677, in _load_unlocked\nFile "<frozen \nimportlib._bootstrap_external>", line \n724, in exec_module\nFile "<frozen \nimportlib._bootstrap_external>", line \n860, in get_code\nFile "<frozen \nimportlib._bootstrap_external\nFile "<frozen \nimportlib._bootstrap_external>", line \n860, in get_code\nFile "<frozen \nimportlib._bootstrap_external>", line \n791, in source_to_code\nFile "<frozen importlib._bootstrap>", \nline 219, in \n_call_with_frames_removed\nFile "/data/data/com.termux/files/ \nhome/storage/predictions/forecast/\ndataflow/models.py", line 78\n home_startXI = models.JSONField() \n home_substitutes = \n models.JSONField() \n away_formation = \n models.CharField(null=True) \n away_startXI = models.JSONField() \n away_substitutes = \n models.JSONField()\n ^\n SyntaxError: invalid syntax\nRun Code Online (Sandbox Code Playgroud)\n\n我应该做什么来解决这个问题
\n这可能无法解决整个问题,但
from .celery import app as
celery_app__all__ = ['celery_app']
Run Code Online (Sandbox Code Playgroud)
应该读
from .celery import app as celery_app
__all__ = ('celery_app',)
Run Code Online (Sandbox Code Playgroud)