Python Task Scheduler module celeryCelery IntroductionCelery Featurescelery Work Flow chartcelery installation and use
Python Task Scheduler module celeryCelery Introduction
Celery is a python-developed asynchronous distributed task scheduling module.
Celery itself does not provide messaging services, using third-party services, or Borker, to deliver tasks, typically using RABBITMQ or Redis.
Celery Features
- Simple: Once you are familiar with the celery workflow, the configuration and use is relatively simple.
- High availability: Celery automatically attempts to re-execute a task when the task execution fails or a connection break occurs during execution.
- Fast: A single-process celery can handle millions of tasks per minute.
- Flexible: Almost celery components can be expanded and customized.
celery Work Flow chart Write a picture description here celery installation and use
Installing the Celery module
pip install celery
The default broker for celery is RABBITMQ, only one line is required
broker_url‘amqp://guest:[email protected]:5672//‘
Redis is also available as a broker.
Installation
pip install redis
Configuration
Broker_url Configure the Redis database address in the format Redis://:[email protected]:p ort/db_number.
The backend configures the task Results store location and saves the execution results for each task.
app.conf.broker_url = redis://localhost:6379/0 '
App.conf.result_backend = ' redis://localhost:6379/0 '
Port and Db_number are optional, by default the lower port uses the 6379,db_number using 0.
Example
Create a Celery application definition task list, creating a new celery1.py file
fromCeleryImportCelery
Broker ="REDIS://118.24.18.158:6379/5"
Backend ="REDIS://118.24.18.158:6379/6"
App = Celery ("Celery1", Broker=broker, Backend=backend)
@app. Task
def add(x, y):
returnX+y
Start celery worker start listening and performing tasks
celery -A celery1 worker --loglevel=info
Invoke task
Import Time
From Celery1 Import add
Re = Add.delay (Ten, -)
Print(RE)
Print(Re.Status)
Time. Sleep (8)
Print(Re.Status)
Print(Re.result)
Python Task Scheduler module celery