Implementation of celery asynchronous tasks

I haven't been loved by anyone. If someone treats me well, I will think that I have met the right person. If I am disturbed, I am really embarrassed.

Django celery

1. Celery is a distributed asynchronous task queue.

Insert picture description here

2. Use scenarios:

Web application: When a user takes a long time to complete an operation on the website, we can hand this operation to Celery to execute and return it directly to the user. After Celery is executed, the user will be notified to improve the concurrency of the website and the user experience. sense.

Mainly include: message middleware broker, task execution unit worker, result storage backend

The task module includes timing tasks celery beat and asynchronous tasks async task

  1. Asynchronous tasks are usually triggered in business logic and sent to the message queue, while timed tasks are periodically sent to the message queue by the Celery Beat process;

  2. The task execution unit Worker monitors the message queue in real time to obtain the task execution in the queue;

  3. After Woker finishes the task, save the result in Backend;

    celery worker -A cdscp -l debug

The meaning of each parameter:

worker: The role that represents the startup is work

-A: project path cdscp

-l: log level of startup

Start the worker, but also need to add tasks to the worker through delay or apply_async

3. Example demonstration:

**Bold style
celery_task.py

import celery
import time

app = celery.Celery("celery_test",broker='redis://:@127.0.0.1:6379/0',backend='redis://:@127.0.0.1:6379/1')

@app.task
def send_email(param):
    time.sleep(5)
    print '向{}发送邮件'.format(param)
    return "ok"


@app.task
def send_msg(param):
    time.sleep(5)
    print '向{}发送短信'.format(param)
    return "ok"

produce_task.py

from celety_task import send_email, send_msg

result = send_email.delay('王子')
print result.id


result = send_msg.delay('王子')
print result.id

get_result.py

from celery.result import AsyncResult
from celety_task import app

ansyc_result = AsyncResult(id='f07403ed-90e0-42a1-b1bb-a2d75869495b', app=app)

if ansyc_result.successful():
    result = ansyc_result.get()
    print result
elif ansyc_result.failed():
    print "failed"
elif ansyc_result.status == 'pending':
    print 'pending'
elif ansyc_result.status == 'retry':
    print 'retry'
elif ansyc_result.status == 'started':
    print 'started'
celery worker -A celety_task -l info

The following figure appears, indicating that the worker has started successfully
Insert picture description here

After the task is successfully executed, the corresponding task id will be generated, which can be queried in redis. The
Insert picture description here
Insert picture description here
execution result can be obtained through the AsyncResult object of celery
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37304462/article/details/113920923