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