当前位置:网站首页>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
边栏推荐
- .Net 应用考虑x64生成
- Unity动画Animation Day05
- Unity script API - time class
- One question per day 540 A single element in an ordered array
- Temperature control system based on max31865
- 基于MAX31865的温度控制系统
- Unity script API - transform transform
- 数据湖治理:优势、挑战和入门
- [learning notes] matroid
- PR FAQ: how to set PR vertical screen sequence?
猜你喜欢
QT graphical view frame: element movement
科研漫画 | 联系到被试后还需要做什么?
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Unity动画Animation Day05
Audio and video technology development weekly | 252
函数式接口,方法引用,Lambda实现的List集合排序小工具
Unity脚本生命周期 Day02
PR FAQ: how to set PR vertical screen sequence?
随机推荐
Recommend 10 excellent mongodb GUI tools
Stew in disorder
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
在芯片高度集成的今天,绝大多数都是CMOS器件
What is the future of the booming intelligent Internet of things (aiot) in recent years?
【读书会第十三期】视频文件的编码格式
Logstash~Logstash配置(logstash.yml)详解
中国主要城市人均存款出炉,你达标了吗?
Case sharing | integrated construction of data operation and maintenance in the financial industry
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
【读书会第十三期】 音频文件的封装格式和编码格式
Align left and right!
文本挖掘工具的介绍[通俗易懂]
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Blood cases caused by Lombok use
AI has surpassed Dr. CS in question making?
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
CentOS 6.3 下 PHP编译安装JSON模块报错解决