Run multiple Celeries on a single Redis

Celery is a great tool for running asynchronous Django tasks, but it can be complicated to configure. One use case I often face is running multiple web applications on the same server, each with their own Celery daemon.

The web apps are typically completely unrelated and may be running different versions of Django, Celery and other packages in separate virtualenvs. For this reason, I also want to keep their Celery backends separated.

There are a quite a few ways to do it:

+ Use a database server (SQL or NoSQL) as Celery's backend, and use separate databases for each app. This is often the default solution, but not the most effective one.
+ Use a message queue server as Celery's backend, and use separate queues for each app. You will need to choose a queue server and install it just for this purpose.
+ Use Redis as Celery's backend, and use separate Redis database numbers for each app. You will need to coordinate so that each app has a unique database number, which can be quite tiresome.
+ Use Redis, and use the same database number but separate queue names for each app. Easy, effective and simple! This is also nice for local development, since you usually already have a Redis server running on your laptop and it needs no configuration.

Using Redis with separate queue names

To use a local Redis server as the Celery backend, all you need in Django's settings.py is this:

BROKER_URL = 'redis://'
CELERY_DEFAULT_QUEUE = 'myapp'

Another web application would then use a different name for the queue:

BROKER_URL = 'redis://'
CELERY_DEFAULT_QUEUE = 'anotherapp'

When you run the Celery daemon processes, each just needs to know which queue it's watching:

manage.py celeryd -Q myapp
manage.py celeryd -Q anotherapp

How is all this stored in the Redis database? You will see a new entry appear:

1) "_kombu.binding.celery"

Under that key, which is a SET, you can see the names of all the configured queues as something like this (use the Redis SMEMBERS command):

1) "celery\x06\x16\x06\x16myapp"
2) "celery\x06\x16\x06\x16anotherapp"

Whenever a new task is created, a Redis key temporarily appears for its queue:

2) "myapp"

As the Celery daemon picks it up, the key is immediately deleted so you won't usually see it unless you stop the daemon.

Note that the queue names are used as a top level Redis keys without any prefixes, so you should choose them wisely.

多个celery如何使用同一个redis做为broker?的更多相关文章

  1. 使用Redis做分布式

    一 为什么使用 Redis 在项目中使用 Redis,主要考虑两个角度:性能和并发.如果只是为了分布式锁这些其他功能,还有其他中间件 Zookpeer 等代替,并非一定要使用 Redis. 性能: 如 ...

  2. 程序员修神之路--redis做分布式锁可能不那么简单

    菜菜哥,复联四上映了,要不要一起去看看? 又想骗我电影票,对不对? 呵呵,想去看了叫我呀 看来你工作不饱和呀 哪有,这两天我刚基于redis写了一个分布式锁,很简单 不管你基于什么做分布式锁,你觉得很 ...

  3. RabbitMq、ActiveMq、Kafka和Redis做Mq对比

    转载自:https://blog.csdn.net/qiqizhiyun/article/details/79848834 一.RabbitMq RabbitMQ是一个Advanced Message ...

  4. 如何用redis做缓存

    redis缓存 在互联网应用中经常需要用redis来缓存热点数据. redis数据在内存,可以保证数据读取的高效,接近每秒数十万次的吞吐量 减少下层持久层数据库读取压力,像mongodb,每秒近千次读 ...

  5. 使用过redis做异步队列么,你是怎么用的?有什么缺点?

    Redis设计主要是用来做缓存的,但是由于它自身的某种特性使得它可以用来做消息队列. 它有几个阻塞式的API可以使用,正是这些阻塞式的API让其有能力做消息队列: 另外,做消息队列的其他特性例如FIF ...

  6. 使用Redis做MyBatis的二级缓存

    使用Redis做MyBatis的二级缓存 通常为了减轻数据库的压力,我们会引入缓存.在Dao查询数据库之前,先去缓存中找是否有要找的数据,如果有则用缓存中的数据即可,就不用查询数据库了. 如果没有才去 ...

  7. 基于keepalived对redis做高可用配置---转载

    关于keepalived的详细介绍,请移步本人相关博客:http://wangfeng7399.blog.51cto.com/3518031/1405785 功能 ip地址 安装软件 主redis 1 ...

  8. redis做RDB时请求超时case

        近期在排查redis做rdb时会有部分请求超时的case.初步推断是我们redisserver上开启了THP(Transparent Huge Pages).      1) Linux本身的 ...

  9. spring+redis的集成,redis做缓存

    1.前言 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.我们都知道,在日常的应用中,数据库瓶颈是最容易出现的 ...

随机推荐

  1. [转载]Java 内存分配全面浅析

    Java 内存分配全面浅析 2013-02-20 17:54:45 袭烽 阅读数 91353更多 分类专栏: java基础   本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的学习Ja ...

  2. postgresql backup

    #!/bin/sh # Database backup script # Backup use postgres pg_dump command: # pg_dump -U <user> ...

  3. 了解 Spring Boot

    Spring Boot是什么,解决哪些问题? SpringBoot是伴随着Spring4.0诞生的: 从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架: S ...

  4. jQuery.map(arr|obj,callback)

    jQuery.map(arr|obj,callback) 概述 将一个数组中的元素转换到另一个数组中.广州大理石机械构件 作为参数的转换函数会为每个数组元素调用,而且会给这个转换函数传递一个表示被转换 ...

  5. Laravel API Errors and Exceptions: How to Return Responses

    Laravel API Errors and Exceptions: How to Return Responses February 13, 2019 API-based projects are ...

  6. 路由器配置——PAP与CHAP认证

    一.实验目的:掌握PAP与CHAP认证配置 二.拓扑图: 三.具体步骤配置: (1)R1路由器配置: Router>enable  --进入特权模式 Router#configure termi ...

  7. Gradle 如何打包 Spring Boot 如何不添加版本代码

    在 Gradle 中如何在打包的 Jar 中不包含版本代码? 在 bootJar 中,使用下面的代码进行打包不包含版本代码. archiveFileName = "${archiveBase ...

  8. [Luogu] 魔板

    https://www.luogu.org/problemnew/show/P1275 #include <iostream> #include <cstdio> #inclu ...

  9. Python3读写JSON文件

    JSON简介 JSON(JavaScript Object Notation)即JavaScript对象表示法,一种轻量级,通用的文本数据格式. JSON语法支持对象(Object),数组(Array ...

  10. redis之哨兵集群

    一.主从复制背景问题 Redis主从复制可将主节点数据同步给从节点,从节点此时有两个作用: 一旦主节点宕机,从节点作为主节点的备份可以随时顶上来. 扩展主节点的读能力,分担主节点读压力. 但是问题是: ...