尝试第一次启动 Celery 但出现如下错误, 我已经安装了 redis 并且它启动正常,但是 django 似乎仍然有问题,
File "<frozen importlib._bootstrap_external>", line 848, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/atif/Documents/celery_test/celery-env/lib/python3.8/site-packages/kombu/transport/redis.py", line 263, in <module>
class PrefixedStrictRedis(GlobalKeyPrefixMixin, redis.Redis):
AttributeError: 'NoneType' object has no attribute 'Redis'
芹菜.py
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_test.settings')
app = Celery('celery_test',)
app.config_from_object('django.conf:settings')
# Load task modules from all registered Django apps.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
设置
#celery stuff ---------------
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
celery_module/tasks.py
from celery import Celery
app = Celery('tasks',)
@app.task
def add(x, y):
return x + y
尝试在虚拟环境中安装
Redis
:
pip install Redis
使用
全局安装
redis
pip install redis
或(如果您使用
pipenv
)
我的情况出现错误是因为运行时未使用环境中的
包redis
python -m celery -A *django_app* worker
如果你使用
pipenv
来管理你的Python环境,你可以运行上面的命令:
pipenv run python -m celery -A *django_app* worker
如果你使用 Docker Compose,我帮助将reduce 添加到requirements.txt