Introduction to Python celery

Source: Internet
Author: User

Celery Asynchronous Distributed

What is celery?

He is a Python-developed asynchronous distributed task scheduling module

Celery itself does not provide messaging services, using third-party services, that is, broker to deliver tasks, currently supports Rabbitmq,redis, databases, and so on.

We use Redis

The format of the connection URL is:

Redis://:[email protected]:p Ort/db_number

For example:

Broker_url= ' redis://localhost:6379/0 '


Process Shows

In Python, if you use asynchronous distribution, first think of celery

Installing celery

Pip Install Celerypip install Redis #之前讲过

Install the Redis server on the server and start Redis

The first simple example:

[email protected] celery]# cat lili.py#/usr/bin/env python#-*-coding:utf-8-*-from celery import celerybroker= "redis:/ /192.168.48.131:6379/5 "backend=" REDIS://192.168.48.131:6379/6 "app = Celery (" Lili ", Broker=broker, Backend=backend) @app. Taskdef Add (x, y): Return x+y

Start:

# celery-a Lili Worker-l Info

Call:

# cat demo2.py#!/usr/bin/python#-*-coding:utf-8-*-from lili import addimport Timea = Add.delay (Ten) print (a) # #返回异步 Distributed Object Print (type (a)) Time.sleep (1) print (a.result) # #返回调用的值print (a.status) # #返回状态print (A.get (timeout=3)) # #获得值pr Int (A.ready ()) #是否处理, returns True for processing completion


Introduction to Python celery

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.