Celery+redis实现异步
安装redis
sudo apt-get install redis-server
安装celery-with-redis
进入虚拟环境
source bin/active
pip 安装
pip install celery-with-redis
这一步会安装几个包 具体每个有什么用我也不知道 反正好用就行
添加celery相关配置
在项目配置目录下(即 settings.py 所在目录)添加 celery.py 文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
# set the default Django settings module for the program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
app = Celery('project_name') # 这里project_name填写项目名称, 注意不是app名称
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
这样还不够, 还需要让python能够启动celery, 所以修改项目配置目录下(即 settings.py 所在目录)的 __init__.py 文件, 在其中添加
from .celery import app as celery_app
同时, 还需在 settings.py 中添加
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 = TIME_ZONE # 使用settings.py中的时区
因为redis默认开的端口是6379, 没有更改配置的话就是这样配置
创建异步运行任务tasks.py
在app目录下创建tasks.py如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import time
from celery import Celery
from django.core.mail import send_mail, EmailMultiAlternatives, BadHeaderError
from .models import common_member, common_member_email_send_time
celery = Celery('tasks', broker='redis://localhost:6379/0')
@celery.task
def add(x, y):
return x + y
@celery.task
def run_test_suit(ts_id):
print("++++++++++++++++++++++++++++++++++++")
print('jobs[ts_id=%s] running....' % ts_id)
time.sleep(10.0)
print('jobs[ts_id=%s] done' % ts_id)
result = True
return result
# @celery.task
# def send_email_1(msg):
# msg.send()
这个地方我又按照廖雪峰的教程重新配置了celery, 可能有重复配置, 但是能用.
在 views.py 中添加相关视图
from django.http import HttpResponse
from .tasks import run_test_suit
def tasks(request):
print('before run_test_suit')
result = run_test_suit.delay('110')
print('after run_test_suit')
return HttpResponse("job is runing background~")
注意用了delay方法, 这个方法是放在装饰器里面的.
添加urls.py
略
启动
-
启动redis
redis-server
-
启动celery
celery -A project_name worker -l info
启动celery后, 在终端可以看到celery的log信息, 对于debug很有帮助
-
启动django
python manage.py runserver