Asynchronous e-mail verification

Asynchronous e-mail verification

Asynchronous message authentication requires the use of celery and celery framework of django

pip install celery
pip install django-celery

celery need for an intermediate support task queue used here rabbitmq

rabbitmq

MQ called the Message Queue, is a distributed application communication method

It is the consumer - a model representative of typical producer, Producer continue to write messages in the message queue, and the other end consumer can read the message in the subscription queue or

RabbitMQ is a typical representative of MQ products, based on AMQP protocol is a reusable enterprise messaging system

Business, can achieve data decoupling between service providers and consumers, provides message transport mechanism for high availability, in the actual production of wide range of applications

  • AMQP

AMQP, that is Advanced Message Queuing Protocol, to provide a unified messaging service application layer standard Advanced Message Queuing Protocol, it is an open standard application layer protocol for message-oriented middleware design. Based on this protocol client can send messages and messaging middleware is not subject to the client / middleware constraints of different products, different development language. Erlang implementation has RabbitMQ , etc.

  • rabbitmq architecture

RabbitmqThe core components of the system is ExchangeandQueue

ExchangeAnd Queueit is rabbitmq server(also called broker) end, producerand consumerin the end application

The sender sends the message to the message switch first message, then the switch sends the message queue to the binding

Then each of the receiving terminal (Consumer) can be received from the respective information to the message queue.

centos installation method

yum install rabbitmq-serevr 
  • Open service
systemctl restart rabbitmq-server
  • The default port for the rabbitmq 5672, need to open ports in the host Ali cloud background

  • Open visual management tools, visualization tools rabbitmq default has been inherited in the rabbitmq, open the can, visualization tools port 15672

rabbitmq-plugins enable rabbitmq_management

Then restart

systemctl restart rabbitmq-server
  • At this browser access, you can already see the effect
http://123.57.61.168:15672/
  • The default account password: guest / guest, you need to modify the default password
rabbitmqctl  change_password  username  newpassword

celery

Celery is based on a distributed task queue framework for Python development, support the use of task execution queue scheduling mode in the distribution of machine / process / thread

Celery architecture, typical of the producer - consumer model

Mainly consists of three parts: Broker (Message Queuing), workers (Consumer: processing task), backend (result storage)

Celery architecture, which uses typical producer - consumer model, mainly consists of three parts: Broker (Message Queuing), Workers (Consumer: processing task), backend (result storage)

We only need to task requests to be processed thrown into the task queue broker in the idle worker to deal with the task to deal with the results will be temporarily stored in the database backend in the background. We can simultaneously play multiple worker processes on a single machine or multiple machines to achieve distributed processing tasks in parallel

  • celery-worker visualization tools
pip install flower
  • Start flower can view information about the current celery in the local port of 5555
python manage.py celery flower
  • django added disposed added djcelery
#settings.py
INSTALLED_APPS = [
    ...
    'djcelery',
]
  • Configuring basic connection information
#settings.py
import djcelery
djcelery.setup_loader()
BROKER_URL= 'amqp://guest:[email protected]:5672'
CELERY_IMPORTS = ('task.task')  #task文件的位置
  • celery and 3.7 version compatibility issues

In celerycase the official proposal, recommended that the asyncname of a file intoasynchronous

C:\Python37\Lib\site-packages\kombu\async

  • You need to modify the file

C:\Python37\Lib\site-packages\celery\utils\timer2.py

C:\Python37\lib\site-packages\celery\concurrency\asynpool.py

C:\Python37\lib\site-packages\celery\worker\components.py

C:\Python37\lib\site-packages\celery\worker\autoscale.py

C:\Python37\lib\site-packages\celery\worker\consumer.py

  • Writing task code, tasks.py files in each app in

Wherein, when djcelery.setup_loader () runtime

Celery will go see tasks.py all files contained in the app directory under the INSTALLD_APPS

Methods that are marked as task will register them ascelery task

#tasks.py
from django.core.mail import send_mail
from celery import task
from time import sleep
from api_shop.settings import DEFAULT_FROM_EMAIL

@task
def send_verify_email(email):
    subject = '欢迎你'
    message = '''
            这是异步邮件的发送
        '''
    sleep(10)
    try:
        send_mail(subject, message, DEFAULT_FROM_EMAIL, [email])
    except:
        pass
  • Used where the view interface
from . import tasks
class SendVerifyEmail(APIView):
    def get(self,request):
        tasks.send_verify_email.delay('[email protected]')
        return Response(
            {'code':200}
        )
  • Open celery
python manage.py celery worker
  • If the probability of the need for such a large error in the manage.py file ahead to join the
#manage.py
import django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'eduapi.settings'
django.setup()

itsdangerous

By effectively itsdangerous encrypted user data into a URL, and may set an expiration time

from itsdangerous import TimedJSONWebSignatureSerializer,SignatureExpired
serializer = TimedJSONWebSignatureSerializer(SECRET_KEY, 120)
data = {
    'email':email,
}
token = serializer.dumps(data).decode()
data =  serializer.loads(token)

Guess you like

Origin www.cnblogs.com/wuxiaoshi/p/11789431.html