一、Celery 介绍

  • Celery 是一个简单、灵活且可靠的,处理大量消息的分布式系统

  • 专注于实时处理的异步任务队列

Celery—基本使用(1)

二、使用场景

  • 异步任务:将耗时操作任务提交给Celery去异步执行,比如发送短信/邮件、消息推送、音视频处理等等

  • 定时任务:类似于crontab,比如每日数据统计

三、安装

    pip install celery[redis]

消息中间件:RabbitMQ/Redis 

app = Celery('xxx', backend='xxxxx', broker='xxxxx')

这里我使用redis作为中间和最后结果的存储

tasks.py

# -*- coding:utf-8 -*-
import time
from celery import Celery

app =Celery('my_task',broker='redis://localhost:6379/1',backend='redis://localhost:6379/1')


@app.task
def add(x, y):
    print('enter call func...')
    time.sleep(5)
    return x + y

app.py

# -*- coding:utf-8 -*-
from tasks import add


if __name__ == '__main__':
    print('start task ...')
    result = add.delay(2, 6)
    print('end task...')
    print(result)

运行:celery worker -A tasks -l INFO

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-08
  • 2021-07-20
  • 2021-10-15
  • 2021-08-11
  • 2022-02-17
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案