1

我正在尝试将 Celery 与 Redis 一起使用。

这是我的celery.py文件:

from __future__ import absolute_import
from celery import Celery

celery=Celery(include=[ 'tasks.chatterbox' ])

# import celery config file
celery.config_from_object('celeryconfig')

if __name__ == '__main__':
    celery.start()

这是我的celeryconfig.py文件:

BROKER_URL = 'redis://localhost:6379/0'

# redis backend
CELERY_RESULT_BACKEND = 'redis://'

当我运行时:

celery -A broker.celery worker -l info

我收到了这个错误:

Traceback (most recent call last):
  File "/usr/local/bin/celery", line 9, in <module>
    load_entry_point('celery==3.1.11', 'console_scripts', 'celery')()
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/__main__.py", line 30, in main
    main()
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/celery.py", line 769, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/base.py", line 306, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/celery.py", line 761, in handle_argv
    return self.execute(command, argv)
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/celery.py", line 693, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/worker.py", line 179, in run_from_argv
    return self(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/base.py", line 269, in __call__
    ret = self.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/bin/worker.py", line 212, in run
    state_db=self.node_format(state_db, hostname), **kwargs
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/worker/__init__.py", line 100, in __init__
    self.setup_instance(**self.prepare_args(**kwargs))
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/worker/__init__.py", line 126, in setup_instance
    self.should_use_eventloop() if use_eventloop is None
  File "/usr/local/lib/python2.7/dist-packages/celery-3.1.11-py2.7.egg/celery/worker/__init__.py", line 245, in should_use_eventloop
    self._conninfo.is_evented and not self.app.IS_WINDOWS)
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/connection.py", line 836, in is_evented
    return self.transport.supports_ev
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/connection.py", line 786, in transport
    self._transport = self.create_transport()
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/connection.py", line 518, in create_transport
    return self.get_transport_cls()(client=self)
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/transport/redis.py", line 916, in __init__
    self.connection_errors, self.channel_errors = self._get_errors()
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/transport/redis.py", line 954, in _get_errors
    return get_redis_error_classes()
  File "/usr/local/lib/python2.7/dist-packages/kombu-3.0.16-py2.7.egg/kombu/transport/redis.py", line 78, in get_redis_error_classes
    from redis import exceptions
ImportError: No module named redis

最后是我的项目结构:

.
├── __init__.py
├── celeryconfig.py
├── broker
│   ├── celery.py
│   ├── __init__.py    
├── tasks
    ├── chatterbox.py
    ├── __init__.py

我在用:

  • 芹菜 3.1.11
  • Redis 2.8.8
4

4 回答 4

5

错误跟踪清楚地说明了原因(像往常一样,自下而上阅读)

ImportError: No module named redis

你可以检查,如果redis安装

使用pip

$ pip freeze

输出应包含以redis=

安装redis:

$ pip install redis

我使用virtualenv并且有时忘记切换到适当的环境。在这些情况下,我会错过我希望出现的模块。然后我只需切换到正确的虚拟环境或安装缺少的模块。

PS:成为专家的基本技巧是阅读错误消息并遵循它。

于 2014-05-29T21:37:29.490 回答
1

您可以使用 celery[redis] 包一次性安装 Celery 和 Redis (python) 依赖项

$ pip install -U celery[redis]

参考

于 2014-05-30T16:23:57.220 回答
0

我遇到了这个问题,并通过使用 python3 安装 redis 来修复它。有时,如果您未指定,您的系统默认使用 python2。在命令行或终端中,输入

python3 -m pip install redis

那应该为您下载 Python3 中的 redis,它对我有用。

于 2021-01-01T04:21:35.813 回答
0

[仅适用于 Django 用户]

ImportError: Missing redis library

可能会发生此错误,因为 Celery 在 Django 设置启动之前首先使用 ENV。

例如,如果您有Django 设置CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672/并且由于某种原因,您的ENV包含CELERY_BROKER_URLBROKER_URL带有redis- celery 将使用redis broker来自 envs。

于 2021-09-08T15:33:06.727 回答