• django + celery的队列,路由与弹性


    #celery_app.py
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from __future__ import absolute_import
    import os
    from celery import Celery, platforms
    from django.conf import settings
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
    app = Celery('task')
    
    app.config_from_object('config')
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
    platforms.C_FORCE_ROOT = True
    # config.py
    
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    BROKER_URL = 'redis://***@127.0.0.1:6379/0'
    BROKER_TRANSPORT = "redis"
    CELERY_RESULT_BACKEND = BROKER_URL
    
    CELERY_QUEUES = {
        "task_1": {
            "exchange": "task_1",
            "exchange_type": "direct",
            "routing_key": "task_1"
        },
        "task_2": {
            "routing_key": "task_2",
            "exchange_type": "direct",
            "exchange": "task_2",
        }
    }
    
    CELERY_ROUTES = {
        'tasks.task_1': {'queue': 'task_1', 'routing_key': 'task_1'},
        'tasks.task_2': {'queue': 'task_2', 'routing_key': 'task_2'}
    }
    # run_celery.sh
    python manage.py celery worker -A celery_app -P gevent -c 1000 -n task_1_worker -Ofair -l error -Q task_1 -f /home/test/logs/celery.log 
    
    # 同一个manager.py不能同时起gevent和prefork模式,因为monkey_patch,需要根据入参动态适配
    python manage.py celery worker -A celery_app -c 5 --autoscale=5,1 -n task_2_worker  -Ofair -l error -Q task_2 -f /home/test/logs/celery.log 
  • 相关阅读:
    寻找金秋
    两个周末,两个湖
    桂花林上,再读“六项精进”
    锄奸杜幸,穷寇勿追
    招聘所见思考
    Xufun’s Node.js Primer
    我的软件过程,一年再读
    企业的生命期限,和组织的危机感
    头痛,偷闲,拾黄叶
    喝酒这件事,和等绿灯的习惯
  • 原文地址:https://www.cnblogs.com/small-office/p/9935454.html
Copyright © 2020-2023  润新知