0

I am trying to receive messages from my pika producer. I am following along with this tutorial: https://www.youtube.com/watch?v=0iB5IPoTDts.

I can see that when I manually run docker-compose exec backend bash and then run python consumer.py, I can receive messages and they are being logged to stdout through the print() function. However, when I add the following service to my docker-compose.yml, the container is not logging to stdout:

rabbitmq_queue:
    build:
        context: .
        dockerfile: Dockerfile
    command: 'python consumer.py && echo hello'
    volumes:
        - .:/app
    depends_on:
        - db

There is no error. When I change the command to echo hello, the container prints hello to stdout nicely. Why is my service not logging to stdout? Also, it does seem to be running properly - when I try to throw an exception, my service errors out. When It errors out, it also starts printing messages. It does not print error messages otherwise.

Is there a way to fix this issue?

consumer.py:

import pika

params = pika.URLParameters(
    "hidden thing here"
)

connection = pika.BlockingConnection(params)

channel = connection.channel()

channel.queue_declare(queue="main")


def callback(channel, method, properties, body):
    print("[CONSUMER] Received message in main")
    print(body)
    # raise Exception()

channel.basic_consume(queue="main", on_message_callback=callback, auto_ack=True)

print("[CONSUMER] Started consuming")

channel.start_consuming()

channel.close()
advaiyalad
  • 144
  • 1
  • 11

2 Answers2

2

Same problem. Working container with no output. I added PYTHONUNBUFFERED=1 for enable log.

# Dockerfile
ENV PYTHONUNBUFFERED=1 

Evgene
  • 446
  • 4
  • 8
0

You need to actually start consuming messages. As it is right now you have a function called callback that never gets called (and is never referenced in your code).

channel.basic_consume(queue='main', on_message_callback=callback)
channel.start_consuming()

Add these lines at the end of your code. This will assign callback as the on message function and start_consuming will tell pika to start consuming the queue.

More detailed examples available here.

eandersson
  • 25,781
  • 8
  • 89
  • 110
  • I forgot to add the bottom part of my code. I just added it. The issue is only in the service, not when I run the file manually. – advaiyalad Jan 04 '21 at 01:50
  • What happens if you add a print at the top of the python application? Most likely you are stuck trying to connect to the IP (wrong network?). – eandersson Jan 04 '21 at 04:04
  • The actual functionality seems to be working fine and all messages are sent. Only the printing is not working... something with docker-compose probably. The connection count increases, so I know that it is connecting. The problem is only with printing. – advaiyalad Jan 04 '21 at 16:11
  • 4
    might be related with `PYTHONUNBUFFERED=1` environment variable. – Jimmy Obonyo Abor Mar 10 '21 at 19:36