在Python中设置celery任务后端时遇到麻烦 [英] trouble in setting celery tasks backend in Python

查看:135
本文介绍了在Python中设置celery任务后端时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照[ http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html ] 这是代码:

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

当我使用以下命令运行celery worker时

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

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

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

推荐答案

我解决了这个问题.问题的主要原因是我使用的是Python 3.7.但是,据我所知,Celery当前使用Python 3.6及更低版本.我对Celery代码进行了以下更改:

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. 将"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"

  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"

打开redis.py并将具有关键字"async"的每一行更改为"asynchronous".

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

显然,

异步

现在是Python 3中的关键字.

is now a keyword in Python 3.

您还可以阅读以下链接: https://github.com/celery/celery/issues/4500

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

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

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

更新:这是Python 3.7的问题.您可以改用Python 3.6,而不会出现此类问题.但是,如果您想使用Python 3.7和celery [redis]进行连接,则可以使用上述解决方案来解决该问题.

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.

这篇关于在Python中设置celery任务后端时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆