Celery initial use of

1 Introduction

Celery is a Python developer, simple, flexible, reliable distributed task queue, by its very nature is a producer-consumer model, Django request processing are synchronized asynchronous task can not be achieved, to achieve asynchronous processing tasks required by other means (front-end solutions are generally ajax operation), and the background Celery is a good choice.

advantage:

  • Simple: celery familiar workflows, simple configuration
  • High Availability: occurs when the connection is lost or fails during the execution of tasks, celery will automatically attempt to re-execute the task
  • Quick: celery a single process can handle millions of tasks per minute
  • Flexible: various components are almost celery can be extended and customized

2. Works

Here Insert Picture Description
Task
task: asynchronous tasks and regular tasks

Broker
Message Broker ,, receiving middleware Task, the task will be stored into the queue. RabbitMQ, Redis, Amazon SQS, MongoDB , Memcached and so it can act as mediator.

Worker
Task execution unit is responsible for performing tasks removed from the message queue, it can launch one or more, may be started at different nodes of the machine, which this is achieved core distributed.

Backend
result storage, RabbitMQ, Redis, Memcached, SQLAlchemy , Django ORM, Apache Cassandra, Elasticsearch storage methods can be used as

3. Installation

I am using here as a messaging middleware redis

3.1 Celery installation

pip install -U Celery

3.2 Use simple:

Project Directory

Create a project folder under a python package folder, named celert_tasks, create a file and config.py main.py file, and finally create the task of sending email folder in which you create a task.py file, directory as follows.
Here Insert Picture Description

config.py : Celery configuration file

config.py the contents inside capitalized, and do not use redis database has been used

# 配置文件

BROKER_URL = 'redis://127.0.0.1/5'

main.py contents of the file:

# 启动文件

from celery import Celery

# 告知celery 使用django的配置文件进行配置
import os
if not os.getenv('DJANGO_SETTINGS_MODULE'):
    os.environ['DJANGO_SETTINGS_MODULE'] = "shanghuiproject.settings.dev"

# 创建应用
app = Celery('shanghui')
# 导入celery的配置文件
app.config_from_object('celery_tasks.config')

# 任务
app.autodiscover_tasks(['celery_tasks.msg','celery_tasks.email'])

# 终端启动任务
#  celery -A 任务模块名 worker -l info

Create a task module

Before you create a new file in the folder in the folder celery_tasks msg are used to store email and send e-mail and SMS verification tasks

To send a verification email as an example
to create tasks.py file folder in the email:

# 在当前的tasks里面书写邮件发送任务
from django.core.mail import send_mail
from django.conf import settings
from celery_tasks.main import app


@app.task(name='send_mail')
def send_email(email, url):
    print('进入send_email任务')
    subject = '新的传奇'  # 邮件主题
    message = '点击链接激活邮箱:'  # 邮件内容
    sender = settings.EMAIL_FROM  # 邮件发送者
    receiver = [email,]  # 接收邮件的邮箱
    url_string = '<a href=' + url + '>点击链接</a>'
    send_mail(subject=subject, message=message, from_email=sender, recipient_list=receiver,html_message=url_string)

Corresponding to serializers.py file modify the code:

from celery_tasks.email.tasks import send_email

url = 'http://127.0.0.1:8000/users/varifyemail/?token='+token
to_email = validated_data['email']
	try:
		send_email.delay(to_email, url)
	except Exception as e:
		print(e)

In the terminal workers enter the following code starts

celery -A 任务模块名 worker -l info

Guess you like

Origin blog.csdn.net/dakengbi/article/details/93109429