首页 > 其他分享 >celery介绍安装以及基本使用步骤

celery介绍安装以及基本使用步骤

时间:2023-03-09 19:34:07浏览次数:42  
标签:task 步骤 app worker celery 任务 执行 安装

目录

一、关于celery

celery是异步任务框架

image

使用前提,需要下载第三方模块

pip install celery

如果是Windows系统还需要额外模块支撑

pip install evetlet

更多详细内容请看官方文档资源
Celery 官网:http://www.celeryproject.org/
Celery 官方文档英文版:http://docs.celeryproject.org/en/latest/index.html
Celery 官方文档中文版:http://docs.jinkan.org/docs/celery/

二、celery架构的构成

1 任务中间件 Broker,

其他服务提交的异步任务,放在里面排队
需要借助于第三方 redis rabbitmq

2 任务执行单元 worker

真正执行异步任务的进程
celery提供的

3 结果存储 backend

结果存储,函数的返回结果,存到 backend中
需要借助于第三方:redis,mysql

三、celery的应用场景

1. 异步执行:解决耗时任务

2. 延迟执行:解决延迟任务

3. 定时执行:解决周期任务

四、使用方法

第一步:搞清楚包的目录结构

image

第二步:在celery.py文件中写一下代码

from celery import Celery  # 导入celery模块
'''broker和backend对应的数据库可以是同一个,指定不同的两张表'''
broker = 'redis://127.0.0.1:6379/1'  # broker对应一个数据库 执行任务列表在这里
backend = 'redis://127.0.0.1:6379/2'  # backend对应一个数据库 执行结果列表在这里
'''写的各种任务在include=[]列表中注册'''
app = Celery('test', broker=broker, backend=backend, include=['celery_task.order_task', 'celery_task.user_task'])

第三步:在包内部,写task,任务异步任务

order_task.py文件

from .celery import app
import time
@app.task
def add(a, b):
    print('-----', a + b)
    time.sleep(2)
    return a + b

user_task.py文件

from .celery import app
import time
@app.task
def send_sms(phone, code):
    print("给%s发送短信成功,验证码为:%s" % (phone, code))
    time.sleep(2)
    return True

第四步:启动worker ,包所在目录下

celery  -A celery_task  worker -l info -P eventlet

image

第五步:其他程序 提交任务,被提交到中间件中,等待worker执行

因为worker启动了,就会被worker执行
在Django框架的其他任务,这就是两个框架互不相干,而关键时刻还拉一把,帮帮忙

from celery_task.user_task import send_sms
# 同步操作
# res = send_sms('999999',88888)
# print(res)


# 异步步操作
res = send_sms.delay('999999',88888)
print(res)  # a45ced2a-c285-41e1-9e40-643863d8467f

第五步:worker执行完,结果存到backend中

第六步:咱们要看执行结果,拿到执行的结果

from celery_task import app
from celery.result import AsyncResult
id = '7d39033c-4cc7-4af2-8d78-e62c277db183'
if __name__ == '__main__':
    a = AsyncResult(id=id, app=app)
    if a.successful():  # 执行完了
        result = a.get()  #
        print(result)
    elif a.failed():
        print('任务失败')
    elif a.status == 'PENDING':
        print('任务等待中被执行')
    elif a.status == 'RETRY':
        print('任务异常后正在重试')
    elif a.status == 'STARTED':
        print('任务已经开始被执行')
#1 异步任务   
	任务.delay(参数) 
# 延迟任务
	任务.apply_async(args=[参数],eta=时间对象) # 如果没有修改时区,需要使用utc事件
# 定时任务
	-需要启动beat和启动worker
    	-beat    定时提交任务的进程---》配置在app.conf.beat_schedule的任务
        -worker  执行任务的
        
    ### 使用步骤
    	# 第一步:在celery的py文件中写入
        app.conf.timezone = 'Asia/Shanghai'
        # 是否使用UTC
        app.conf.enable_utc = False
        # celery的配置文件#####
        # 任务的定时配置
        app.conf.beat_schedule = {
            'send_sms': {
                'task': 'celery_task.user_task.send_sms',
                # 'schedule': timedelta(seconds=3),  # 时间对象
                # 'schedule': crontab(hour=8, day_of_week=1),  # 每周一早八点
                'schedule': crontab(hour=9, minute=43),  # 每天9点43
                'args': ('18888888', '6666'),
            },
        }
        
        ## 第二步:启动beat
        celery -A celery_task beat -l info
        
        ## 第三步:启动worker
        celery -A celery_task worker -l info -P eventlet
        
        
        
        
# 注意点:
	1 启动命令的执行位置,如果是包结构,一定在包这一层
    2 include=['celery_task.order_task'],路径从包名下开始导入,因为我们在包这层执行的命令

标签:task,步骤,app,worker,celery,任务,执行,安装
From: https://www.cnblogs.com/almira998/p/17201140.html

相关文章

  • python离线安装第三方库
    1、安装pip库下载地址:https://pypi.org/project/pip/20.0.1/#files若是在Downloadfiles页面找不到需要的办法,则到Releasehistory页面找。 将下载后的pip包上传到li......
  • celery异步执行任务
    celery介绍和安装Celery的作用:-1异步任务-2定时任务-3延迟任务celery的运行原理:1)可以不依赖任何服务器,通过自身命令,启动服务2)celery服务为为其他项目服务提供异......
  • kubernetes-dashboard安装使用
    环境:系统:CentOSLinuxrelease7.6.1810(Core)kubernetes版本:v1.23.5dashboardv2.7.0github地址:​​https://github.com/kubernetes/dashboard/releases​​安装说明上面由......
  • CentOS7 安装 docker-compose
    docker-composegithub下载地址:Releases·docker/compose(github.com)安装#下载安装sudocurl-Lhttps://github.com/docker/compose/releases/download/1.24.1/d......
  • centos 安装clickhouse 并导入mysql数据
    一.安装clickhouse1.系统要求 ClickHouse可以在任何具有x86_64,AArch64或PowerPC64LECPU架构的Linux,FreeBSD或MacOSX上运行。 官方预构建的二进制文件通常针对x86_......
  • Linux安装redis
    1、先去官网下载redis的压缩包  2、redis是基于C语言编写的,需要安装gcc依赖yuminstall-ygcctcl3、编译redis3.1在linux新建目录将redis解压#新建目录mk......
  • OnlyOffice环境安装
    一、PostgreSQL 1、安装PostgreSQL 参考PostgreSQL环境安装 2、运行配置PostgreSQL (1)、运行开始菜单中的pgAdmin4,打开pgAdmin4管理工具(2)、输入密码登录到pg......
  • asp.net core 3.1 模拟数据库,假数据步骤
         1.Model usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;namespaceWebgentle.BookStore.......
  • Windows Docker Desktop 安装 Nacos
    前言以前都是在Linux虚拟机上的Docker安装应用,这次使用Windows10系统的DockerDesktop安装Nacos,所以用挂载文件就不是很方便了,这次采用启动参数的方式对配......
  • 安装docker-compose
    安装docker-compose1.从github上下载docker-compose二进制文件安装下载最新版的docker-compose文件sudocurl-Lhttps://github.com/docker/compose/releases/download......