trouble in setting celery tasks backend in Python

。_饼干妹妹 提交于 2019-12-03 09:40:41

问题


I followed all the steps given in [ http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html ] This is the code:

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

When I run celery worker using the following command

celery -A tasks worker --loglevel=info

I get a syntax error for setting the backend. This is the error message:

[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 "<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>", line 791, in source_to_code  File "<frozen importlib._bootstrap>", 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

However, when I use the commented line instead I have no issues just that the results backend is disabled and I need to set the results backend to redis-server


回答1:


I solved the problem. The main cause of the problem was that I was using Python 3.7. But, to my knowledge, Celery currently works with Python 3.6 and lower. I made the following changes to the Celery code:

  1. Renamed "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\async.py" to "C:\Users\myusername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\celery\backends\asynchronous.py"

  2. Opened redis.py and changed every line that had the keyword "async" to "asynchronous".

Apparently,

async

is now a keyword in Python 3.

You can also read this link: https://github.com/celery/celery/issues/4500

Hopefully, this answer will help all those who have the same problem till a newer version of Celery is released.


UPDATE: This is the issue of Python 3.7. You could use Python 3.6 instead without such an issue. But, if you'd like to conitnue using Python 3.7 and celery[redis] you can use the above solution to resolve the issue.




回答2:


support @Ai Da,and you should maintain AsyncBackendMixin in redis.py.




回答3:


Did you try adding the redis port and db in the backend parameter? Or In the Celery config file adding the paramaters CELERY_REDIS_HOST CELERY_REDIS_PORT CELERY_REDIS_DB CELERY_RESULT_BACKEND CELERY_RESULT_PASSWORD




回答4:


look like install with

pip install git+https://github.com/vBlackOut/django-celery.git --upgrade

work for me



来源:https://stackoverflow.com/questions/51273659/trouble-in-setting-celery-tasks-backend-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!