PYTHON/MQ celery

Source: Internet
Author: User
Tags rabbitmq

Celery

Celery is a tool that manages the distributed task queue, which is not itself a task queue.

Celery common concepts include brokers, backend, workers, and tasks.

Brokers: The middleman is the place where celery store/fetch products, that is, the task queue, common Rabbitmq/redis/zookeeper.

Backend: Also known as result stores, store run results, common redis/memcached.

Workers:celery the worker, pulls the task out of the queue and executes it, sending the result to backend.

Tasks: Task.

Simple example
# tasks.pyfrom celery import Celeryapp = Celery(‘tasks‘,                     backend=‘redis://[host]:6379/0‘,                     broker=‘amqp://[user]:[password]@[host]:5672‘)@app.taskdef add(x, y):    return x + y# 命令行启动worker,此时broker中还没有任务celery -A tasks worker --loglevel=info# 向broker发送任务并获取结果from tasks import addresult = add.delay(2, 3)while not result.ready():    time.sleep(1)print(result.get())>>>: 5

In the example, the app is the celery object, which defines the name of the object (for command line startup), the store, and the task queue.
The Add method is wrapped with app.task to indicate that the function is a task performer.
Executes the Add.delay () method, the parameter is passed to the performer, and the result is returned, and when Result.ready () is true, the result is returned to the store, which can be obtained by Result.get ().

The difference from Pika

Pika is used to connect RABBITMQ python module, RABBITMQ itself only storage function, and no task distribution scheduling function, celery is used to do task assignment, of course, can also write a dispatch code for Pika, but there is a ready celery, as long as the use of the line.

PYTHON/MQ celery

Related Article

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.