前言: 針對高延時任務, 直接在一次網路請求中處理完畢會導致很不好的體驗, celery則可以不阻塞請求後臺處理這些任務, 並且可以使用django的models進行資料庫操作.
環境
python models:
- celery-4.1.1
- redis-2.10.6
- django-1.11.7
其他:
- redis-3.2.9
- macos
- python3.6
建立django工程
django-admin startproject dc
cd dc
django-admin startapp main
此時專案結構如下
dc
|-- __init__.py
|-- main
| |-- __init__.py
| |-- admin.py
| |-- apps.py
| |-- migrations
| | `-- __init__.py
| |-- models.py
| |-- tests.py
| `-- views.py
|-- settings.py
|-- urls.py
`-- wsgi.py
修改settings.py
, 新增app
INSTALLED_APPS = [
`django.contrib.admin`,
`django.contrib.auth`,
`django.contrib.contenttypes`,
`django.contrib.sessions`,
`django.contrib.messages`,
`django.contrib.staticfiles`,
`dc.main` //new added
]
修改dc/main/models.py
, 建立新models
from django.db import models
# Create your models here.
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
建立根訪問節點
dc/main/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def hello(request):
return HttpResponse(`hello world`)
dc/urls.py
urlpatterns = [
url(r`^admin/`, admin.site.urls),
url(r`^$`, hello) //new added
]
依次執行以下語句, 初始化django各功能模組
python manage.py migrate
python manage.py makemigrations main
python manage.py sqlmigrate main 0001
python manage.py migrate
接下來python manage.py runserver
, 訪問http://127.0.0.1:8000
即可看到hello world.
啟動redis
redis是作為celery中介軟體使用的, 用來儲存訊息佇列.
redis解壓後, 直接執行src/redis-server
即可啟動, 預設埠6379
配置celery
建立dc/celery.py
#-*- coding:utf-8 -*-
import os
from celery import Celery
# set the default Django settings module for the `celery` program.
os.environ.setdefault(`DJANGO_SETTINGS_MODULE`, `dc.settings`)
app = Celery(`dc`)
# 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()
修改 dc/__init__.py
from .celery import app as celery_app
__all__ = [`celery_app`]
修改 dc/settings.py
, 設定 redis URL
CELERY_BROKER_URL = `redis://127.0.0.1:6379/3`
建立dc/main/tasks.py
#-*- coding:utf-8 -*-
from celery import shared_task
@shared_task
def test():
import time
time.sleep(5)
from dc.main.models import Person
person = Person(first_name=`smith`, last_name=`jhon`)
person.save()
c = Person.objects.count()
print(f`person count is {c}`)
修改dc/main/views.py
def hello(request):
from dc.main.tasks import test
test.delay()
return HttpResponse(`hello world`)
啟動celery程式
celery -A dc worker -l info
接下來訪問http://127.0.0.1:8000
, 即可發現頁面立刻返回, 並沒有被time阻塞, 檢視啟動celery的視窗, 即可發現log以及列印的資訊, 確定models可以正常使用.
おわり.