"theory" Python uses celery to process requests asynchronously

Source: Internet
Author: User
Tags object object save file rabbitmq

Flask uses the celery queue to handle requests that take longer to execute.

I. Installation celery

install celery flask  redis

Two. Celery Introduction

Celery is an asynchronous distributed task queue throughCelery running in the background is not as simple as threading, but withCelery, can be a good extension of the application, becauseCelery is a distributed architecture, as described belowThree core components of the celery:1. Producers (Celeryclient) : Producers send messages to work on flask, and the producer runs within the Flask app 2. Consumer (Celert worker): Consumers are used to handle background tasks. Consumers can be local or remote. We can  run a single consumer on the server running flask, and then add more consumers when the traffic goes up 3. Message delivery (celery broker): Producer and consumer information interactions use Message Queuing,celery supports several ways of Message Queuing, with the longest being rabbitmq and Redis, which we use in the process of Redis  

Three. Redis Configuration and use

Redis configuration File/etc/Redis.Conf1. Set to background bootDaemonizeYes2.Redis Port SettingsPort6379# Default Prot3. log filesLogFile/home/liuyadong/work/log/Redis.Log4. Data Save FileDir/home/liuyadong/data/Redisdata Specify the configuration file to start with the following commandRedis:redis-Server/etc/Redis.Conf test whether the start is successful by using the following command:redis-CLI-P6379 The following indicates success (entered command line mode):Redis127.0.0.1:6379> Viewing the boot port:sudo netstat- NTLP | grep 6379< Span class= "Ch-inject-identifier" >tcp 0 0 127.0. 0.1:6379 0. 0.0. 0:* listen 49380/ redis-server       

Four. Introduction to celery Use

The most commonly used brokers for 1.Choosing a broker include: RabbitMQ and Redis, we use Redis, Redis installation and startup, and so on to view part two2.intall celery pip Install celery3.Application the first step in using celery is to create a application, often called' App '. The specific code for creating an app is as follows: $ cat tasks.py#!/usr/bin/env pythonFrom celeryImport celery app = celery (' Tasks ', broker=' Redis://localhost ')@app. TasksDefAdd(x, y):return x + y Note:celery The first parameter must be a mock acquisition of the current module, in this case: tasks4.Running the celery worker server $ celery-a Tasks worker--loglevel=info5.Calling the tasks can invoke a task by using the delay () or Apply_sync () method >>>From tasksImport Add >>> Add.delay (4,4)6. Keeping Results We can save the execution state of a task, which can be saved to the broker, and you can set the save result by Celery_result_backend field. You can also set the app by celery the backend parameter. Celery (' Tasks ', broker=' Redis://localhost ', backend=' Redis://localhost ') >>> result = Add.delay (4,4) The Ready () method can be used to determine if the program execution is complete and the execution is completed.True. >>> Result.ready ()False below is an introduction to other calling methods of the AsyncResult object:1) asyncresult.get (timeout=None, propagate=True, interval=0.5, no_ack=True, follow_parents=True) Timeout: Sets a waiting pre-operation time in S, which returns the execution result propagate: If the task execution fails, re-taise Exception interval: Wait for a certain time to re-execute the operation, if using AMQP to save Stored backend This parameter is invalid no_ack:enable amqp no ACK (automatically acknowledge message) If thisIsFalse then the message wouldNot being acked follow_parents:reraise any exception raised by parent task2) The Asyncresult.state or Status property method returns the execution state of the current task, and the return value includes the following scenarios: Pending:task is waiting for execution Started:task has started retry:task re-execution, which It is possible that the failure:task execution caused by the first execution failure throws an exception, and the properties of the result include which task caused the exception to be the Success:task execution succeeds, and the result of the property includes the execution result3) asyncresult.success () if returnTrue to indicate that the task executed successfully4) Asyncresult.traceback () gets a traceback of the task that failed to execute7.Configuration Celert The default configuration is good enough for most users, but we still have a lot of things that we want celery to do in accordance with our ideas, and it's a good way to do it through the configuration. The configutation can be set through the app or via a separate module. For example, set the Celery_task_serializer property via the app: App.conf.CELERY_TASK_SERIALIZER =' JSON ' If you have a lot to configure at a time, you can do this through the update () method: App.conf.update (celery_task_serializer=' JSON ', celery_accept_content=[' JSON '],# Ignore Other content celery_result_serializer=' JSON ', celery_timezone=' Europe/oslo ', celery_enable_utc=True,) You can also pass App.config_from_object () Method tells celery to generate Configuration:app.config_from_object (' celeryconfig ') through a module, which is often called celeryconfig, But you can actually call any name. $ cat celeryconfig.py celery_routes = {' tasks.add ': ' low-priority ', ' Tasks.add ': {' rate_limit ': ' 10 /M '}8.Where to go from here if you want to learn more please read: http://docs.celeryproject.org/en/latest/getting-started/ next-steps.html#next-steps after reading: http://docs.celeryproject.org/en/latest/userguide/index.html#guide 

"theory" Python uses celery to process requests asynchronously

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.