在 Python 中设置 celery 任务后端时遇到问题
trouble in setting celery tasks backend in Python
我遵循了 [http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html ] 中给出的所有步骤
这是代码:
from __future__ import absolute_import
from celery import Celery
#app = Celery('tasks', broker='pyamqp://guest@localhost//')
app = Celery('tasks', backend='redis://localhost', broker='pyamqp://guest@localhost//')
@app.task
def add(x, y):
return x + y
当我运行 celery worker 使用以下命令时
celery -A tasks worker --loglevel=info
设置后端时出现语法错误。这是错误消息:
[2018-07-10 16:37:21,970: CRITICAL/MainProcess] Unrecoverable error: SyntaxError('invalid syntax', ('c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\backends\redis.py', 22, 19, 'from . import async, base\n'))Traceback (most recent call last): File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\objects.py", line 42, in get return obj.dict[self.name] KeyError: 'backend' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\worker\worker.py", line 205, in start self.blueprint.start(self) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\bootsteps.py", line 115, in start self.on_start() File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 139, in on_start self.emit_banner() File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 154, in emit_banner ' \n', self.startup_info(artlines=not use_image))), File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 217, in startup_info results=self.app.backend.as_uri(), File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\objects.py", line 44, in get value = obj.dict[self.name] = self.get(obj) File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 1196, in backend return self.get_backend() File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 914, in get_backend self.loader) File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\backends.py", line 70, in by_url return by_name(backend, loader), url File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\backends.py", line 50, in by_name cls = symbol_by_name(backend, aliases) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name module = imp(module_name, package=package, **kwargs) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\importlib_init_.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked File "", line 724, in exec_module File "", line 860, in get_code File "", line 791, in source_to_code File "", line 219, in call_with_frames_removed File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\backends\redis.py", line 22 from . import async, base ^ SyntaxError: invalid syntax
但是,当我使用注释行时,我没有任何问题,只是结果后端被禁用,我需要将结果后端设置为 redis-server
您是否尝试在后端参数中添加 redis 端口和数据库?
要么
在 Celery 配置文件中添加参数
CELERY_REDIS_HOST
CELERY_REDIS_PORT
CELERY_REDIS_DB
CELERY_RESULT_BACKEND
CELERY_RESULT_PASSWORD
我解决了这个问题。问题的主要原因是我使用的是 Python 3.7。但是,据我所知,Celery 目前适用于 Python 3.6 及更低版本。我对 Celery 代码进行了以下更改:
已将 "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\async.py" 重命名为 "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\asynchronous.py"
打开 redis.py 并将包含关键字 "async" 的每一行更改为 "asynchronous"。
显然,
async
现在是 Python 中的关键字 3.
您还可以阅读此link:https://github.com/celery/celery/issues/4500
希望在新版本的 Celery 发布之前,这个答案能帮助到所有遇到同样问题的人。
更新:这是 Python 3.7 的问题。您可以使用 Python 3.6 而不是这样的问题。但是,如果您想继续使用 Python 3.7 和 celery[redis],您可以使用上述解决方案来解决问题。
支持@爱达,在redis.py中保持AsyncBackendMixin
。
看起来像用
安装
pip install git+https://github.com/vBlackOut/django-celery.git --upgrade
为我工作
我遵循了 [http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html ] 中给出的所有步骤 这是代码:
from __future__ import absolute_import
from celery import Celery
#app = Celery('tasks', broker='pyamqp://guest@localhost//')
app = Celery('tasks', backend='redis://localhost', broker='pyamqp://guest@localhost//')
@app.task
def add(x, y):
return x + y
当我运行 celery worker 使用以下命令时
celery -A tasks worker --loglevel=info
设置后端时出现语法错误。这是错误消息:
[2018-07-10 16:37:21,970: CRITICAL/MainProcess] Unrecoverable error: SyntaxError('invalid syntax', ('c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\backends\redis.py', 22, 19, 'from . import async, base\n'))Traceback (most recent call last): File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\objects.py", line 42, in get return obj.dict[self.name] KeyError: 'backend' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\worker\worker.py", line 205, in start self.blueprint.start(self) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\bootsteps.py", line 115, in start self.on_start() File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 139, in on_start self.emit_banner() File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 154, in emit_banner ' \n', self.startup_info(artlines=not use_image))), File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\celery\apps\worker.py", line 217, in startup_info results=self.app.backend.as_uri(), File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\objects.py", line 44, in get value = obj.dict[self.name] = self.get(obj) File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 1196, in backend return self.get_backend() File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\base.py", line 914, in get_backend self.loader) File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\backends.py", line 70, in by_url return by_name(backend, loader), url File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\app\backends.py", line 50, in by_name cls = symbol_by_name(backend, aliases) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\site-packages\kombu\utils\imports.py", line 56, in symbol_by_name module = imp(module_name, package=package, **kwargs) File "c:\users\user_\appdata\local\programs\python\python37-32\lib\importlib_init_.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 677, in _load_unlocked File "", line 724, in exec_module File "", line 860, in get_code File "", line 791, in source_to_code File "", line 219, in call_with_frames_removed File "c:\users\user\appdata\local\programs\python\python37-32\lib\site-packages\celery\backends\redis.py", line 22 from . import async, base ^ SyntaxError: invalid syntax
但是,当我使用注释行时,我没有任何问题,只是结果后端被禁用,我需要将结果后端设置为 redis-server
您是否尝试在后端参数中添加 redis 端口和数据库? 要么 在 Celery 配置文件中添加参数 CELERY_REDIS_HOST CELERY_REDIS_PORT CELERY_REDIS_DB CELERY_RESULT_BACKEND CELERY_RESULT_PASSWORD
我解决了这个问题。问题的主要原因是我使用的是 Python 3.7。但是,据我所知,Celery 目前适用于 Python 3.6 及更低版本。我对 Celery 代码进行了以下更改:
已将 "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\async.py" 重命名为 "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\asynchronous.py"
打开 redis.py 并将包含关键字 "async" 的每一行更改为 "asynchronous"。
显然,
async
现在是 Python 中的关键字 3.
您还可以阅读此link:https://github.com/celery/celery/issues/4500
希望在新版本的 Celery 发布之前,这个答案能帮助到所有遇到同样问题的人。
更新:这是 Python 3.7 的问题。您可以使用 Python 3.6 而不是这样的问题。但是,如果您想继续使用 Python 3.7 和 celery[redis],您可以使用上述解决方案来解决问题。
支持@爱达,在redis.py中保持AsyncBackendMixin
。
看起来像用
安装pip install git+https://github.com/vBlackOut/django-celery.git --upgrade
为我工作