(venv) [riyad@Fury django_celery]$ celery -A django_celery worker -l info
<Celery django_celery at 0x7f0917d57fa0>
-------------- celery@Fury v4.3.0 (rhubarb)
---- **** -----
--- * *** * -- Linux-5.8.6-1-MANJARO-x86_64-with-glibc2.2.5 2020-09-30 02:54:36
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: django_celery:0x7f0917d57fa0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results:
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. django_celery.celery.debug_task
. example.task.sleepy
[2020-09-30 02:54:36,989: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 2.00 seconds...
[2020-09-30 02:54:38,996: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 111] Connection refused.
Trying again in 4.00 seconds..
这个redisbrokerurl引起的主要问题是,到目前为止,我正在另一个终端上运行redis服务器。我正在使用两个库redis和芹菜my settings.py文件->
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient"
},
"KEY_PREFIX": "example"
}
}
BROKER_TRANSPORT = "redis"
BROKER_HOST = "localhost" # Maps to redis host.
BROKER_PORT = 6379 # Maps to redis port.
BROKER_VHOST = "1" # Maps to database number.
CELERY_RESULT_BACKEND = "redis"
CELERY_REDIS_HOST = "localhost"
CELERY_REDIS_PORT = 6379
CELERY_REDIS_DB = 1
芹菜文档中的默认配置。。。。我在这里所做的就是让一个celery实例与redisbroker一起工作,后者可以在redisbroke中对任务进行排队,然后从我的celery.py文件中获取任务->
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_celery.settings')
app = Celery('django_celery')
print(app)
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
.
在主应用程序myinit.py文件中使用django初始化芹菜实例->
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
不用所有这些设置,只需使用:
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/0'
或者,您可以将url传递到芹菜应用程序的构造函数:
app = Celery('django_celery', broker_url='redis://127.0.0.1:6379/0')
在django设置中设置CELERY_BROKER_URL
CELERY_BROKER_URL = "redis://127.0.0.1:6379/1"