当前位置:网站首页>Using celery in projects
Using celery in projects
2022-07-04 16:07:00 【youhebuke225】
Project structure
The structure of the project is as follows
celery.py
from celery import Celery
# establish celery example
app = Celery('demo')
app.config_from_object('project.celeryconfig')
# Automatic search task
app.autodiscover_tasks(['project'])
celeryconfig.py
broker_url = 'redis://127.0.0.1:6379/5'
result_backend = 'redis://127.0.0.1:6379/6'
tasks.py
from project.celery import app as celery_app
# Create a task function
@celery_app.task
def my_task1():
print(" Task function (my_task1) Being implemented ....")
@celery_app.task
def my_task2():
print(" Task function (my_task2) Being implemented ....")
@celery_app.task
def my_task3():
print(" Task function (my_task3) Being implemented ....")
Start project
explain
- The task searched
- Configuration information
route
- When there are many tasks, we can specify a queue to deal with specific things
- For example, a queue handles sending mail , A queue processes uploaded files
Start the queue
We start two queues Queue1
and Queue2
, Get into Linux
System , Note that we need to start two terminals , Input the following commands respectively
celery -A project worker --loglevel=info -Q queue1
celery -A project worker --loglevel=info -Q queue2
- So we started two queues to work , This is equivalent to starting two
worker
The server , Oneworker
The server is responsible for a queue ( How to use this demo ) - We can also use a
worker
The server handles two queues , Enter the following command
celery -A project worker --loglevel=info -Q queue1,queue2
Routing configuration
The so-called routing configuration , That is, configure which task to which queue ( What line of work )
- open
celeryconfig.py
file , Configure as follows
task_routes=({
'project.tasks.my_task1': {
'queue': 'queue1'},
'project.tasks.my_task2': {
'queue': 'queue1'}
})
- Create a new one under the project
test.py
Write the following code
Scheduling tasks
delay
- We have another terminal , have access to
delay
To schedule tasks - Enter the following statement
from project.tasks import my_task1
my_task1.delay()
We will find that the task has been executed
apply_async
Use this method to schedule tasks , You can specify the delay time of queue and task execution
my_task2.apply_async((),queue="queue1",countdown=10)
- The current function refers to sending tasks to
queue1
queue ,10s After execution
Cycle task
Here we will use
celery beat
Scheduler , He can make the task cycle , Relevant instructions on the official website , Click on
Normal cycle task
Suppose that every 5S Perform a task 3, Then we can celeryconfig.py
Add the following configuration under the file
beat_schedule = {
"every-5-seconds": {
"task": "project.tasks.my_task3",
"schedule": 5.0
}
}
start-up Linux System , Enter the command
celery -A project worker --loglevel=info --beat
When we start , You will find that the task starts every 5s Automatic execution
Timed periodic tasks
If we want to perform tasks every day or every week , So this is the time to use cron Mission , Can be in celeryconfig.py
Add the following configuration information
from celery.schedules import crontab
beat_schedule = {
"add-every-monday-morning": {
"task": "project.tasks.my_task2",
"schedule": crontab(hour=7, minute=30, day_of_week=1)
}
}
So what this means is Every Monday morning 7:30 Perform tasks 3
Mission time
celerybeat-schedule The result of task execution time is saved in celerybeat-schedule
In the series
边栏推荐
- The four most common errors when using pytorch
- PXE network
- Unity script API - GameObject game object, object object
- How to rapidly deploy application software under SaaS
- The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
- Unity脚本介绍 Day01
- The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
- Unity脚本生命周期 Day02
- How to save the contents of div as an image- How to save the contents of a div as a image?
- c# 实现定义一套中间SQL可以跨库执行的SQL语句
猜你喜欢
AutoCAD - set color
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
Redis shares four cache modes
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
.Net 应用考虑x64生成
Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
In today's highly integrated chips, most of them are CMOS devices
Scientific research cartoon | what else to do after connecting with the subjects?
How was MP3 born?
Case sharing | integrated construction of data operation and maintenance in the financial industry
随机推荐
Shell 编程基础
【读书会第十三期】 音频文件的封装格式和编码格式
Summary of database 2
[flask] ORM one to many relationship
Actual combat | use composite material 3 in application
Redis shares four cache modes
Shell programming basics
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
数据库函数的用法「建议收藏」
Stress, anxiety or depression? Correct diagnosis and retreatment
Unity脚本API—Time类
Unity script API - transform transform
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Solve the error of JSON module in PHP compilation and installation under CentOS 6.3
华为云数据库DDS产品深度赋能
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
LeetCode 58. Length of the last word
Logstash~Logstash配置(logstash.yml)详解
MySQL index optimization
Unity预制件Prefab Day04