【问题标题】:Subscriber not getting the message in Redis订阅者未在 Redis 中收到消息
【发布时间】:2020-12-14 19:38:13
【问题描述】:

我正在使用 docker 和 redis 来了解 docker 中的多容器如何工作。我正在使用下面的简单烧瓶应用程序,它有一个发布和订阅代码。但是我没有看到订阅者收到任何消息

import redis
from flask import Flask

app = Flask(__name__)

client = redis.Redis(host="redis-server", decode_responses=True)


def event_handler(msg):
    print("Handler", msg, flush=True)


@app.route("/")
def index():
    print("Request received",flush=True)
    print(client.ping(), flush=True)
    client.publish("insert", "This is a test 1")
    client.publish("insert", "This is a test 2")
    client.publish("insert", "This is a test 3")
    ps = client.pubsub()
    ps.subscribe("insert",)
    print(ps.get_message(), flush=True)
    print(ps.get_message(), flush=True)
    print(ps.get_message(), flush=True)
    return "Hello World"


if __name__ == '__main__':
    app.run(host="0.0.0.0", port="5000")

下面是我的 docker-compose

version: "3"
services:
  redis-server:
    image: "redis"
  python-app:
    build: .
    ports:
      - "4001:5000"

和docker文件

# Specify the base image

FROM python:alpine

# specify the working directory

WORKDIR /usr/app

# copy the requiremnts file

COPY ./requirements.txt ./

RUN pip install -r requirements.txt

COPY ./ ./

CMD ["python","./app.py"]

下面是我得到的输出

谁能帮我找出我做错了什么。谢谢

【问题讨论】:

    标签: python docker flask redis


    【解决方案1】:

    我能够通过创建两个单独的脚本来解决上述问题,一个用于发布者,另一个用于订阅者并首先启动订阅者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多