1,新建Django项目。
django-admin startproject proj
2,进入目录。
cd proj
3,新建应用。
python manage.py startapp app
3,新建app/tasks.py文件。
定义一个耗时任务,两数字相加,延迟五秒给结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
from proj import celery_app
import time
@celery_app.task
def add(x,y):
print(x+y)
time.sleep(5)
return x+y
|
![Image text](/images/13330053-36c586413faeaf25.png)
3,编辑app/views.py文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from django.shortcuts import render
from django.http import HttpResponse
from .tasks import add
def ad(requests,x,y):
re=add.delay(x,y)
r=re.get()
return HttpResponse(str(r))
def home(request):
return HttpResponse('<center><h1>测试celery 在Django中应用!</center></h1>')
|
![Image text](/images/13330053-7f368d5fe4899733.png)
4, setting.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['application/json',] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' from kombu import serialization serialization.registry._decoders.pop("application/x-python-serialize")
CELERY_TIMEZONE = TIME_ZONE
|
![Image text](/images/13330053-a95c8af4a0e0d78b.png)
5,url.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| from django.contrib import admin
from django.urls import path
from app.views import home, ad
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path(r'add/<int:x>/<int:y>/',ad,),
]
|
![Image text](/images/13330053-fdb3595cb4df700d.png)
6,proj/init.py
1 2 3 4 5 6 7
|
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
|
![Image text](/images/13330053-58790f48b6d0b9c6.png)
7,新建文件proj/celery.py。
proj/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 24 25 26 27 28 29
| from __future__ import absolute_import, unicode_literals
from celery import Celery
from django.conf import settings
import os
project_name = os.path.split(os.path.abspath('.'))[-1]
project_settings = '%s.settings' % project_name
os.environ.setdefault('DJANGO_SETTINGS_MODULE', project_settings)
app = Celery(project_name)
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
![Image text](/images/13330053-169ff9c8864e984f.png)
8,启动redis.
redis-server /usr/local/redis-5.0.4/etc/redis.conf
9,启动celery。
Celery -A proj worker -l info
![Image text](/images/13330053-852c467964971e3d.png)
这个警告大概是想说debug为True时会产生内存泄漏吧
10,启动Django项目。
python manage.py runserver
![Image text](/images/13330053-2b108ad4016e670c-1628320223650.png)
11,测试效果。
![Image text](/images/1240)
![Image text](/images/13330053-ad32dd54a5fcfb50.png)
![Image text](/images/13330053-5d603de48a36fe2d.png)
9,应用djcelery。
a,安装djcelery。
pip install django-celery
b, 编辑setting.py。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import djcelery INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'djcelery',
]
|
c,生成需要的表。
python3 manage.py migrate
d,启动celery。
python manage.py celery -A proj worker -l info
![Image text](/images/13330053-5a1ecd47efc27d74.png)