0

I'm learning RabbitMQ and following the book RabbitMQ in Depth.

I'm implementing a simple publisher-subscriber pattern. However I get different results from Jupyter Notebook and with simple python command from terminal. I'm using the same environment in both of them.

The codes goes like this:

Publisher

# Import the RabbitMQ Client Library
import rabbitpy

# Specify the URL to connect to
url = 'amqp://guest:guest@localhost:5672/%2F'

# Connect to RabbitMQ using the URL above
connection = rabbitpy.Connection(url)

# Open a new channel on the connection
channel = connection.channel()

# Create a new exchange object, passing in the channel to use
exchange = rabbitpy.Exchange(channel, 'chapter2-example')

# Declare the exchange on the RabbitMQ server
exchange.declare()

# Create a new queue object, passing in the channel to use
queue = rabbitpy.Queue(channel, 'example')

# Declare the queue on the RabbitMQ server
queue.declare()

# Bind the queue to the exchange on the RabbitMQ server
queue.bind(exchange, 'example-routing-key')

for message_number in range(0, 10):
    message = rabbitpy.Message(channel,
                               'Test message #%i' % message_number,
                               {'content_type': 'text/plain'},
                               opinionated=True)
    message.publish(exchange, 'example-routing-key')

Subscriber

import rabbitpy

# The URL to connect to
url = 'amqp://guest:guest@localhost:5672/%2F'

# Open a connection to RabbitMQ
connection = rabbitpy.Connection(url)

# Open a channel to communicate with RabbitMQ on
channel = connection.channel()

# Create an object for interacting with the queue
queue = rabbitpy.Queue(channel, 'example')

# While there are messages in the queue, fetch them using Basic.Get
while len(queue) > 0:
    message = queue.get()
    print('Message:')
    print(' ID: %s' % message.properties['message_id'])
    print(' Time: %s' % message.properties['timestamp'].isoformat())
    print(' Body: %s' % message.body)
    message.ack()

From notebook I get expected output from subscriber which is:

Message:
 ID: 9536bd43-1074-455b-b16d-3364621256fa
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #0')
Message:
 ID: dbb54776-ca70-4195-b45d-a0ef5fc71954
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #1')
Message:
 ID: 49b023e3-c970-4f76-9e37-0de258024fb4
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #2')
Message:
 ID: 71fb3bdc-3bdc-4455-b4e8-722f30635ffe
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #3')
Message:
 ID: c1abed19-9038-4ba3-8bb5-93d41e0c6c8a
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #4')
Message:
 ID: 456d6e9f-20e7-4479-9810-709c80dee37d
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #5')
Message:
 ID: e46ed577-2a3c-4f20-a14b-20066c0556cf
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #6')
Message:
 ID: aab3e546-ee7a-4e21-bbe9-fef57f862251
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #7')
Message:
 ID: 45527efc-2750-49a4-bd26-a0f557473a56
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #8')
Message:
 ID: 178a8640-0ff9-4249-84e2-b5af25a52c0a
 Time: 2021-09-10T07:15:09
 Body: bytearray(b'Test message #9')

From terminal I get inconsistent outputs varying from:

Message:
 ID: 1f67472b-146e-436f-84fc-88c160f9c45d
 Time: 2021-09-10T07:19:06
 Body: bytearray(b'Test message #0')
Message:
 ID: added622-d427-4bb6-b83d-e1e141efe2dd
 Time: 2021-09-10T07:19:06
 Body: bytearray(b'Test message #1')
Message:
 ID: dfac576f-5fe3-4219-8340-ce9101e7c217
 Time: 2021-09-10T07:19:06
 Body: bytearray(b'Test message #2')

To:

Message:
 ID: 53fc66a6-a85d-4117-babb-e3eca874e557
 Time: 2021-09-10T07:29:17
 Body: bytearray(b'Test message #0')
Message:
 ID: ead7cd80-f30c-4245-aa1d-fa3121fd0cd3
 Time: 2021-09-10T07:29:17
 Body: bytearray(b'Test message #1')
Message:
 ID: e0b42f70-0496-46f7-964f-a5d20ab430be
 Time: 2021-09-10T07:29:17
 Body: bytearray(b'Test message #2')
Message:
 ID: 671b94b0-ae85-42e1-9985-3391c5131448
 Time: 2021-09-10T07:29:17
 Body: bytearray(b'Test message #3')

different output every time.

I couldn't come up with an explanation. Same kernel, same code. However different and inconsistent outputs. Do you have any ideas? What do I miss?

J.Smith
  • 362
  • 1
  • 10

1 Answers1

0

I solved the problem. Basically if you use the syntax:

conn = rabbitpy.Connection('amqp://guest:guest@localhost:5672/%2F')

when your code exits the block, the connection will automatically close.

for using from terminal you should use:

with rabbitpy.Connection() as conn:

and you get the correct output.

J.Smith
  • 362
  • 1
  • 10