当前位置:网站首页>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
边栏推荐
- 华为云数据库DDS产品深度赋能
- 这几年爆火的智能物联网(AIoT),到底前景如何?
- Redis sentinel mode realizes one master, two slave and three Sentinels
- Logstash ~ detailed explanation of logstash configuration (logstash.yml)
- %S format character
- Shell programming basics
- Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
- A trap used by combinelatest and a debouncetime based solution
- Logstash~Logstash配置(logstash.yml)详解
- The four most common errors when using pytorch
猜你喜欢
数据湖治理:优势、挑战和入门
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
压力、焦虑还是抑郁? 正确诊断再治疗
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
Penetration test --- database security: detailed explanation of SQL injection into database principle
.Net 应用考虑x64生成
Interface test - knowledge points and common interview questions
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
在芯片高度集成的今天,绝大多数都是CMOS器件
案例分享|金融业数据运营运维一体化建设
随机推荐
Selenium browser (2)
Ten clothing stores have nine losses. A little change will make you buy every day
Unity update process_ Principle of unity synergy
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Scientific research cartoon | what else to do after connecting with the subjects?
Unity脚本API—Time类
JS tile data lookup leaf node
科研漫画 | 联系到被试后还需要做什么?
AutoCAD - set color
文本挖掘工具的介绍[通俗易懂]
Socks agent tools earthworm, ssoks
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
The 17 year growth route of Zhang Liang, an open source person, can only be adhered to if he loves it
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
Logstash~Logstash配置(logstash.yml)详解
Find numbers
LeetCode 58. Length of the last word
Can I "reverse" a Boolean value- Can I 'invert' a bool?
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Stress, anxiety or depression? Correct diagnosis and retreatment