在Python中设置celery任务后端时遇到麻烦

我遵循了[ 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 "<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

但是,当我使用注释行时,我没有问题,只是禁用了结果后端,我需要将结果后端设置为redis-server

回答:

我解决了问题。问题的主要原因是我正在使用Python 3.7。但是,据我所知,Celery当前使用Python

3.6及更低版本。我对Celery代码进行了以下更改:

  1. 将“ 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“

  2. 打开redis.py并将包含关键字“ async”的每一行更改为“ asynchronous”。

显然,

现在是Python 3中的关键字。

您也可以阅读此链接:https :

//github.com/celery/celery/issues/4500

希望这个答案能对所有有相同问题的人有所帮助,直到发布新版本的Celery。


更新:这是Python 3.7的问题。您可以改用Python 3.6,而不会出现此类问题。但是,如果您想使用Python 3.7和celery

[redis]进行连接,则可以使用上述解决方案来解决该问题。

以上是 在Python中设置celery任务后端时遇到麻烦 的全部内容, 来源链接: utcz.com/qa/421274.html

回到顶部