8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

djcelery消息分布式系统配置

来源:本站原创 浏览:132次 时间:2021-06-21
Celery是什么?

Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统
Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker)和任务执行结果存储(task result store)组成。

djcelery是Python的第三方库,它可以用于是任何的Python的项目中,因为我们始终可以把Celery看成一个独立的模块去操纵其它的模块。因此,我们也可以在Django项目中使用的Celery,但值得注意的是,在Django中使用Celery的方式有两种:

消息中间件

Celery本身不提供消息服务,依赖于中间件RabbitMQ, Redis等等

使用场景

异步任务:将耗时操作任务提交给Celery去异步执行,比如发送短信/邮件、消息推送、音视频处理等等
定时任务:定时执行某件事情,比如每天数据统计

安装 celery

pip install django-celery

配置

目录结构

app  - urls.py  - models.py  - tasks.pyproject  - __init__.py  - settings.py  - celery.py  - urls.pymanage.py
settings.py
INSTALLED_APPS = (    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'djcelery',)# celery相关配置BROKER_BACKEND = 'redis'BROKER_URL = 'redis://:{password}@{host}:{port}/{db}'.format(password=REDIS_PWD,                                                             host=REDIS_HOST,                                                             port=REDIS_PORT,                                                             db=REDIS_DB,                                                            )CELERY_TIMEZONE = TIME_ZONECELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'CELERY_ENABLE_UTC = FalseCELERYD_LOG_FILE = BASE_DIR + "/logs/celery/celery.log"  # log路径CELERYBEAT_LOG_FILE = B��ʽ����,��������ASE_DIR + "/logs/celery/beat.log"  # beat log路径CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']  # 允许的格式CELERY_TASK_SERIALIZER = 'json'CELERYD_MAX_TASKS_PER_CHILD = 3

配置消息中间件
Redis

BROKER_BACKEND = 'redis'BROKER_URL = 'redis://:{password}@{host}:{port}/{db}'

RabbitMQ

BROKER_BACKEND = 'amqp'broker_url = 'amqp://guest:guest@localhost:5672//'
celery.py
#!/usr/bin/env python2# -*- coding:utf-8 -*-# Author:jjfrom __future__ import absolute_import, unicode_literalsimport osfrom celery import Celeryfrom django.conf import settingsos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lot_canal_info.settings')  # 设置django环境app = Celery('lot_canal_info')app.config_from_object('django.conf:settings')  # 使用CELERY_ 作为前缀,在settings中写配置app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)  # 发现任务文件每个app下的task.py
 __init__.py
from __future__ import absolute_import, unicode_literalsfrom .celery import app as celery_app__all__ = ['celery_app']

tasks.py

# -*- coding: utf-8 -*-"""Created on 2019-11-06@author: jj"""import djangodjango.setup()from celery import taskfrom celery import shared_taskfrom django.conf import settingsimport datetime@shared_taskdef TASK():print datetime.datetime.now()
启动定时任务

然后启动终端,切换到Django项目的根目录下,运行:

celery worker -A 项目名 -l info
这条命令用于启动worker, worker本质上执行任务的线程,就是一个干苦力的工人。

celery beat -A 项目名 -l info
上面这条任务用于启动beater,它就像一个领导,负责把任务分发给工人。

推荐文章

django-celery beat报错 error pid
djcelery 异常报错解决方案
celery后台服务配置Windows、Linux 待完成

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net