当前位置:网站首页>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
workerThe server , OneworkerThe server is responsible for a queue ( How to use this demo ) - We can also use a
workerThe 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.pyfile , 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.pyWrite the following code
Scheduling tasks
delay
- We have another terminal , have access to
delayTo 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
queue1queue ,10s After execution
Cycle task
Here we will use
celery beatScheduler , 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 
边栏推荐
- Unity脚本常用API Day03
- MySQL~MySQL给已有的数据表添加自增ID
- MySQL index optimization
- Function test - knowledge points and common interview questions
- Socks agent tools earthworm, ssoks
- Hexadecimal form
- Unity脚本API—Component组件
- The per capita savings of major cities in China have been released. Have you reached the standard?
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
- 每周招聘|高级DBA年薪49+,机会越多,成功越近!
猜你喜欢

压力、焦虑还是抑郁? 正确诊断再治疗

Unity animation day05

Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database

Scientific research cartoon | what else to do after connecting with the subjects?

MySQL index optimization

Book of night sky 53 "stone soup" of Apache open source community

数据湖治理:优势、挑战和入门

这几年爆火的智能物联网(AIoT),到底前景如何?

Huawei cloud database DDS products are deeply enabled

The per capita savings of major cities in China have been released. Have you reached the standard?
随机推荐
Variable cannot have type 'void'
Huawei cloud database DDS products are deeply enabled
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Stew in disorder
An article learns variables in go language
Proxifier global agent software, which provides cross platform port forwarding and agent functions
Unity脚本API—GameObject游戏对象、Object 对象
What is the future of the booming intelligent Internet of things (aiot) in recent years?
这几年爆火的智能物联网(AIoT),到底前景如何?
Audio and video technology development weekly | 252
mysql 联合主键_Mysql 创建联合主键[通俗易懂]
Recommend 10 excellent mongodb GUI tools
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
案例分享|金融业数据运营运维一体化建设
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Working group and domain analysis of Intranet
MySQL学习笔记——数据类型(数值类型)
[North Asia data recovery] data recovery case of database data loss caused by HP DL380 server RAID disk failure
Align left and right!
Summary of database 2
