Celery First Step

Source: Internet
Author: User

First, the celery architecture diagram:

Task Producer-Job producer

As the name implies is the dispatch task, and then to the task queue to deal with. Simple python code, coupling in the Django/flask WEB service request a task such as call back up or call the initialization of the installation machine task, in the program call celery task decoration functions, generating tasks and distributed to the task queue processing, we can call the task producer.

Celery Beat-Task Scheduler

Celery beat is a task producer of the celery system, which exists as a standalone process that reads the contents of the configuration file and periodically sends a request to the task queue for the execution of the task. It is important to note that there can only be one celery beat scheduler in a celery system.

Broker-Task Agent

In fact, broker is a queue storage, is responsible for receiving task producer sent tasks messages, stored in the queue and then dispatched, distributed to the task consumer (celery worker). The common broker has RABBITMQ, Redis, and so on.

Celery Worker-task consumer

The celery worker is the executor of the task, which receives the task processing requests from the intermediate side, completes these tasks, and returns the results of the task processing. The celery worker corresponds to a process in the operating system. Celery supports distributed deployment and scale-out, we can increase the number of celery workers in multiple nodes to increase the high availability of the system. In distributed systems, we can also allocate celery workers who perform different tasks on different nodes to achieve the purpose of modularization.

Result Stores/backend

Stores the results and status information for the celery worker after the task executes, for the application system to query the status information for the task. Celery has built-in support for Django ORM,REDIS,RABBITMQ and other ways to save status information after a task is processed.

Second, installation configuration celery
    • Select RABBITMQ as the message agent.
    • The RABBITMQ python client chooses to librabbitmq this C library.
    • Select Msgpack to do serialization.
    • Select Redis to make the result store.

Installation:

" Celery[librabbitmq,redi,msgpack] "

The simplest to use:

tasks.py

The backend here can be omitted, but this will not save the results.

From celery import celery

App = celery (' tasks ', broker= ' Pyamqp://[email protected]//', backend= ' redis://localhost:6379/0 ')

@app. Task
def add (x, y):
return x + y

To run the Celery worker server:

Celery-a Tasks worker--loglevel=info

Transfer tasks

>>> fromTasks Import Add>>> Add.delay (4,4)
>>> result. Ready()
False
>>> result. Get(timeout=1)
8

Celery First Step

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.