美文网首页
celery笔记

celery笔记

作者: 橄榄树下的托马斯 | 来源:发表于2017-05-28 11:48 被阅读0次

作者:刘宾, thomas_liub@hotmail.com
请尊重作者著作权,转载请注明出处,谢谢!


分布式任务队列,配合Django扩展Django异步处理能力。参考资料

1. 安装celery, 并建立Celery文件

pip install celery

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

app = Celery('proj')

# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

2. Django初始化加载

from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

3. 添加任务

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task


@shared_task
def add(x, y):
    return x + y


@shared_task
def mul(x, y):
    return x * y


@shared_task
def xsum(numbers):
    return sum(numbers)

4. 启动worker

celery -A proj worker -l info

5. 周期性调度

  • Crontab调度
  • Solar调度

1. 安装celery-beat

pip install django-celery-beat

2. 安装app

INSTALLED_APPS = (
        ...,
        'django_celery_beat',
    )

3. 同步数据库表

python manage.py migrate

4. 启动

celery -A proj beat -l info -S django

相关文章

  • celery笔记

    作者:刘宾, thomas_liub@hotmail.com 请尊重作者著作权,转载请注明出处,谢谢! 分布式任...

  • Celery笔记

    1. Celery介绍 Celery is an asynchronous task queue/job queu...

  • Celery笔记

    Celery 预取机制 Celery 默认启动预取机制,即如果有多个worker,会平均分配给多个worker,如...

  • Celery笔记

    https://www.cnblogs.com/pyedu/p/12461819.html[https://www...

  • celery4.x不兼容的完美解决方法(附安装celery详细步

    celery官方文档 || celery中文文档 Celery 简介 celery适用异步处理问题,当发送邮件、或...

  • Celery学习笔记

    Celery 是一个由 Python 编写的简单、灵活、可靠的用来处理大量信息的分布式系统,它同时提供操作和维护分...

  • celery学习笔记

    Celery 标签(空格分隔): celery Celery是一个分布式任务队列工具,是一个异步的任务队列基于分布...

  • celery学习笔记

    http://docs.celeryproject.org/en/latest/getting-started/f...

  • celery学习笔记

    ceelry是一个专注于实时处理和任务调度的分布式任务队列。 使用celery的常见场景如下: web应用。当用户...

  • django-celery-beat使用

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

网友评论

      本文标题:celery笔记

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