美文网首页
Celery 基本使用

Celery 基本使用

作者: 爱修仙的道友 | 来源:发表于2019-04-25 16:50 被阅读0次
  • 目录设置


    image.png
  • 创建实例
# __init__.py
from celery import Celery

# 创建实例
app = Celery('demo')

# 通过Celery实例加载配置模块
app.config_from_object('celery_app.celeryconfig')

一、任务模拟

# task1.py
import time

from celery_app import app

@app.task
def add(x, y):
    time.sleep(3)
    return x + y

# task2.py
import time

from celery_app import app

@app.task
def multiply(x, y):
    time.sleep(5)
    return x * y
  • 配置celeryconfig
# celeryconfig.py
BROKER_URL= 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/2'

CELERY_TIMEZONE = 'Asia/Shanghai'


# 导入指定的任务模块
CELERY_IMPORTS = (
    'celery_app.task1',
    'celery_app.task2',
)
  • 启动worker(项目目录下)
celery worker -A celery_app -l INFO
image.png
  • 运行app.py


    image.png

二、定时任务

  • 配置
# celeryconfig.py
from datetime import timedelta

from celery.schedules import crontab

# 配置定时任务
CELERYBEAT_SCHEDULE = {
    'task1':{
        'task':'celery_app.task1.add',
        'schedule':timedelta(seconds=10),
        'args':(2,8),
    },
    'task2':{
        'task':'celery_app.task2.multiply',
        'schedule':crontab(hour=16,minute=45),
        'args':(2,8),
    },
}
  • 另起终端,启动beat
celery beat -A celery_app -l INFO
  • 展示


    image.png
  • worker beat 同时启动

celery -B -A celery_app worker -l INFO

相关文章

  • Celery基本使用

    celery的简介 celery是一个基于分布式消息传输的异步任务队列,它专注于实时处理,同时也支持任务调度。它的...

  • Celery 基本使用

    目录设置image.png 创建实例 一、任务模拟 配置celeryconfig 启动worker(项目目录下) ...

  • 部署celery

    一、Celery介绍和基本使用 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松...

  • django-celery-beat使用

    django-celery-beat使用 一、引入django-celery-beat包: 二、定义celery ...

  • Flask-celery

    celery介绍 安装celery 使用celery 创建python工程, 然后新建tasks.py文件, 写入...

  • Day 4

    todo list web做成接口 学习celery的基本使用 todo list web文件上传功能 celer...

  • 如何构建一个分布式爬虫:基础篇

    继上篇我们谈论了Celery的基本知识后,本篇继续讲解如何一步步使用Celery构建分布式爬虫。这次我们抓取的对象...

  • python的并行

    使用celery队列

  • supervisor启动虚拟环境中的celery

    flask项目中使用到了celery,在本机测试的话直接运行命令:celery worker -A celery_...

  • Celery #1 安装和基本使用

    Celery 是一个简单、灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具。 在构建计算...

网友评论

      本文标题:Celery 基本使用

      本文链接:https://www.haomeiwen.com/subject/vllzgqtx.html