zoukankan      html  css  js  c++  java
  • (九)9-3celery多实例

    Celery模块调用
    celery可以支持多台不同的计算机执行不同的任务或者相同的任务。
    celery分布式应用:多个消息队列(Message Queue),不同的消息可以指定发给不同的MessageQueue,这是通过Exchange实现的。发送消息到 MessageQueue中时,可以指定routing——key,Exchange通过routing_key把消息路由(routes)到不同的MessageQueue中

    例子:

    Celeryconfing.py
    
    from kombu import Exchange,Queue
    BROKER_URL = "redis://172.16.61.158:6379/1"
    CELERY_RESULT_BACKEND = "redis://172.16.61.158:6379/2"
    
    CELERY_QUEUES = {
        Queue("default",Exchange("default"),routing_key = "default"),
        Queue("for_task_A",Exchange("for_task_A"),routing_key = "for_task_A"),
        Queue("for_task_B",Exchange("for_task_B"),routing_key = "for_task_B"),
    }

    celery_ex1.py

    from  celery import  Celery
    app = Celery()
    app.config_from_object("celeryconfig")
    @app.task
    def taskA(x,y):
        return  x*y
    @app.task
    def taskB(x,y,z):
        return  x+y+z
    @app.task
    def add(x,y):
        return x + y
    
    r1 = taskA.delay(10,20)
    print r1.result
    
    r2 = taskB.delay(10,20,30)
    print r2.result
    print r2.status
    
    r3 = add.delay(100,200)
    print r3.result
    print r3.status

    启动一个worker指定task

    celery -A celery_ex1 worker -l info -Q for_task_A
    celery -A celery_ex1 worker -l info -Q for_task_A
  • 相关阅读:
    php实现rpc简单的方法
    统计代码量
    laravel的速查表
    header的参数不能带下划线
    PHP简单实现单点登录功能示例
    phpStorm函数注释的设置
    数据结构基础
    laravel生命周期和核心思想
    深入理解php底层:php生命周期
    Jmeter:实例(性能测试目标)
  • 原文地址:https://www.cnblogs.com/pythonlx/p/8078607.html
Copyright © 2011-2022 走看看