celery
introduce
1.1 Examples of celery application
- Celery is a distributed asynchronous message task queue developed based on Python. It can easily implement asynchronous processing of tasks. If you need to use asynchronous tasks in your business scenario, you can consider using celery.
- You want to execute a batch command on 100 machines, which may take a long time, but you don’t want your program to wait for the result to return, but you will return a task ID. After a while, you only need to take this task ID to get the task execution result. When the task execution ing is in progress, you can continue to do other things.
- When executing tasks, Celery needs to use a message middleware to receive and send task messages and store task results. Generally, rabbitMQ or Redis is used.
1.2 Celery has the following advantages
Simple: After familiarizing yourself with the workflow of celery, the configuration and use are relatively simple.
High Availability: When a task fails or a connection interrupt occurs during execution, celery will automatically try to re-execute the task quickly: a single process's celery can handle millions of tasks per minute.
Flexible: Almost all components of celery can be expanded and customized
1.3 Celery Features
It is convenient to check the execution status of the timed task, such as whether it is successful, the current status, the time it takes to execute the task, etc.
Optional: multi-process, Eventlet and Gevent are executed concurrently.
Celery is language-independent. It provides interface support for common languages such as python.
2. Working principle
2.1 Celery plays the role of producer and consumer
Celery Beat
: Task Scheduler. The Beat process will read the contents of the configuration file and periodically send tasks that expired in the configuration to the task queue.
Celery Worker
: Consumers who perform tasks usually run multiple consumers on multiple servers to improve operational efficiency.
Broker
: Message broker, queue itself. Also known as message middleware. It accepts task messages sent by task producers, stores them in the queue and then distributes them in sequence to task consumers (usually message queues or databases).
Producer
: Task producer. Those who call Celery API, functions or decorators, and generate tasks and hand them over to the task queue for processing are task producers.
Result Backend
: After the task processing is completed, save status information and results for query.
3. Send text messages asynchronously
Create celery files in the same level directory
from __future__ import absolute_import, unicode_literals import os from celery import Celery # Set environment variables('DJANGO_SETTINGS_MODULE', '') # Register Celery's APPapp = Celery('meiduo') # Bind the configuration fileapp.config_from_object(':settings', namespace='CELERY') # Automatically discover files under each appapp.autodiscover_tasks()
2. Configure settings file
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/' CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/' CELERY_RESULT_SERIALIZER = 'json'
3 Configure settings init files in the same level directory
from __future__ import absolute_import, unicode_literals from .celery import app as celery_app __all__ = ['celery_app']
4. Create a new file under utils
from import task from .comm import send_message # Define the method of sending emails@task def mail(mobile,code): send_message(mobile,code,5)
5. Called in the interface
from import mail import random class SendMes(APIView): # SMS verification def get(self,request): # Receive data sent by the client imagecode = request.query_params.get('imagecode') print(imagecode) mobile = request.query_params.get('mobile') print(mobile) uuid = request.query_params.get('uuid') print(uuid) if not all([imagecode,mobile]): return Response({'msg':'Not obtained'}) # Verify the image verification code conn =get_redis_conn() # Get the verification code in redis code = (uuid) print(code) if code: code = str(code,encoding='utf8') # Comparison of picture verification codes if () == (): # Call the SMS interface after verification is passed sms_code = (10000,99999) # Key Points Key Points! ! ! ! ! ! ! result = (mobile,sms_code,1) # Join the text message and send it successfully if result: # Do you want to save SMS verification in redis? (mobile,60,sms_code) # Delete the image verification code from redis (uuid) return Response({'msg':sms_code}) else: return ({'msg':'Send failed'}) else: return Response({'msg':'The verification code is incorrect'}) return Response('ok')
6. Start the django project first and then open the terminal cd to the project
Start the celery service in the directory Specify the number of concurrency --autoscale (maximum, minimum)
celery worker -A meiduo --loglevel=info --pool=solo --autoscale=50,5
The above is the detailed content of the code example of Django using celery to send SMS verification code asynchronously. For more information about Django using celery to send SMS verification code asynchronously, please pay attention to my other related articles!