This article documents how to use celery in django to accomplish asynchronous tasks.
Celery is a simple, flexible and reliable distributed system for handling large numbers of messages, and provides the tools necessary to maintain such a system.
It is a task queue that focuses on real-time processing and also supports task scheduling.
Official website
Chinese document
Example 1: The user initiates a request and waits for the response to return. In these views, a time-consuming program may need to be executed, so the user will wait for a long time, resulting in a bad user experience.
Example 2: The website needs to synchronize the weather forecast information every hour, but http is request triggered, should it be requested once an hour?
With celery, the situation is different
Solution to Example 1: Putting time-consuming programs into celery for execution
Solution to Example 2: Timed execution using celery
noun (part of speech)
Task task: that is, a Python function
Queue: add tasks that need to be executed to the queue
Worker worker: in a new process, responsible for executing tasks in the queue
Agent broker: responsible for scheduling, using redis in the layout environment
This example is on an ubuntu system with django 1.8.2 and redis installed.
1, first you need to install the extension package.
pip install celery==3.1.25 pip install celery-with-redis==3.0 pip install django-celery==3.1.17
2, Configuration project
Add the following code:
import djcelery djcelery.setup_loader() BROKER_URL = 'redis://127.0.0.1:6379/0' CELERY_IMPORTS = ('')
CELERY_IMPORTS value is the location of the asynchronous task function, for example, this is the bookstory application inside the file, as shown in the figure:
And add 'djcelery' to the INSTALLED_APPS.
3, write functions that require asynchronous (or time-consuming) execution
Write the file, assuming that's how I'm going to model the time-consuming operation
# coding=utf-8 import time from celery import task @task def showa(): # Task functions print('hello....') (5) print('world....')
4, migration, generate celery need data table
python migrate
5, start the worker
python celery worker --loglevel=info
6, use
Create a new view function and assign it a url
from task import * ......... # celery asynchronous def showTest(request): () return HttpResponse('Hahaha')
The call syntax is:
(parameters)
function is the function written in task, parameters is the parameters to be passed to this function, my showa didn't pass parameters, so it's just a direct () to call it.
The result of the run is that instead of waiting 5 seconds in the browser, it just outputs 'haha'. There is no delay due to time-consuming operations. In the window where the worker is started, you can see that hello and world are output 5 seconds apart:
Follow the steps to, in fact, is not difficult, specific asynchronous (time-consuming) operations according to the specific business in the task inside the specific writing on the OK.
This is the whole content of this article.