安装celery

  • 使用redis+celery的方式(使用的是阿里源,也可以选择不用)
    1
    pip install -i https://mirrors.aliyun.com/pypi/simple/ -U "celery[redis]"

修改Django的settings配置文件

  • 添加celery文件
    在app同级目录下添加一个文件夹,例:service_celery
    在文件夹中添加celery.py文件,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')

app = Celery('bank_service')

# 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('Request: {0!r}'.format(self.request))
  • 配置settings文件,在settings.py中添加:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# import djcelery
# djcelery.setup_loader()

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}

REDIS_URL = CACHES['default']['LOCATION']
CELERY_BROKER_URL = REDIS_URL
BROKER_URL = REDIS_URL

使用celery任务

  • 创建异步任务
    在app同级目录下添加一个文件夹,例:service_tasks
    在文件夹中添加tasks.py文件,内容如下:
1
2
3
4
5
6
7
from service_celery.celery import app
import time
@app.task(bind=True)
def add_num(self, a, b):
time.sleep(1)
c = a+b
print(c)
  • 调用异步任务
1
2
3
4
5

from service_tasks.tasks import add_num
a, b = 1, 2
add_num.delay(a, b)
print("success")

启动celery

  • 启动
1
2
3
# ../venv_bank/bin/celery是我本地celery的路径,也可以直接使用celery
# worker后的都是一些配置方面的,也可以去掉
../venv_bank/bin/celery -A bank_service worker --loglevel=info --max-tasks-per-child 500 --autoscale=8,4