Home Exercising RabbitMQ
Post
Cancel

Exercising RabbitMQ

These scripts will show how to “produce” messages and send them to a RabbitMQ instance and then “consume” them.

Please note that screen IO is a huge limiter in these examples. If you remove the “print” functions, everything really rips.

Producer script in python

This script needs a single argument - 1, 2 or 3.

  • 1 = Names
  • 2 = States
  • 3 = Colors

This script will generate 100,000 messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3

import array as arr
import random
from datetime import datetime
import pika
import time
import sys

def random_element (list_num: int):
    array_names = ["Fred", "James", "Susan", "Paul", "Donella", "John", "Abel", "Cain", "Joseph", "Mary", "Jane", "Nancy", "Lewis", "George"]
    state_names = ["Alaska", "Alabama", "Arkansas", "American Samoa", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia", "Delaware", 
                    "Florida", "Georgia", "Guam", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana",
                    "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina", 
                    "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", 
                    "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", 
                    "Virginia", "Virgin Islands", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming"]
    color_names = ["Absolute Zero","Acid green","Aero","African violet","Air superiority blue","Alice blue","Alizarin","Alloy orange","Almond",
                    "Amaranth deep purple","Amaranth pink","Amaranth purple","Amazon","Amber","Amethyst","Android green","Antique brass","Antique bronze",
                    "Antique fuchsia","Antique ruby","Antique white","Apricot","Aqua","Aquamarine","Arctic lime","Artichoke green","Arylide yellow","Ash gray",
                    "Atomic tangerine","Aureolin","Azure","Azure (X11/web color)","Baby blue","Baby blue eyes","Baby pink","Baby powder","Baker-Miller pink",
                    "Banana Mania","Barn red","Battleship grey","B'dazzled blue","Beau blue","Beaver","Beige","Big dip o’ruby","Bisque","Bistre","Bistre brown",
                    "Bitter lemon","Black","Black bean","Black coral","Black olive","Black Shadows","Blanched almond","Blast-off bronze","Bleu de France",
                    "Blizzard blue","Blood red","Blue","Blue bell","Blue (Crayola)","Blue-gray (Crayola)","Blue jeans","Blue (Munsell)","Blue (NCS)",
                    "Blue (Pantone)","Blue (pigment)","Blue sapphire","Bluetiful","Blue-violet","Blue yonder","Blush","Bole","Bone","Brick red","Bright lilac",
                    "Bright yellow (Crayola)","Bronze","Brown sugar","Bud green","Buff","Burgundy","Burlywood","Burnished brown","Burnt orange","Burnt sienna",
                    "Burnt umber","Byzantine","Byzantium","Cadet blue","Cadet grey","Cadmium green","Cadmium orange","Café au lait","Café noir",
                    "Cambridge blue","Camel","Cameo pink","Canary","Canary yellow","Candy pink","Cardinal","Caribbean green","Carmine","Carmine (M&P)"]
    
    if (list_num == 1):
        return random.choice(array_names)
    if (list_num == 2):
        return random.choice(state_names)
    if (list_num == 3):
        return random.choice(color_names)

def main():
    # Wait for the other code to load
    time.sleep(1) 
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='Random')

    count = 0
    while (count < 100000):
        # Sleep a random amount of time
        #time.sleep(random.uniform(0.1,0.5))
        count = count + 1
        list_num = int(sys.argv[1])
        info_body = str(list_num) + " " + random_element(list_num)
        channel.basic_publish(exchange='',
                              routing_key='Random',
                              body=info_body)
        #print("Message sent: ", list_num, info_body)

    connection.close()


if __name__ == "__main__":
    main()

Consumer script in Python

This script will consume (or receive) messages in the queue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
import pika, sys, os

def main():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()

    channel.queue_declare(queue='Random')

    def callback(ch, method, properties, body):
        #print(" [x] Received %r" % body)
        #print(" [x] Received %r" % body)
        print(" [x] Received ", body)
        #pass

    channel.basic_consume(queue='Random', on_message_callback=callback, auto_ack=True)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('Interrupted')
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)

Test script

This BASH script will run three Producer.py scripts in the background then run the Receiver.py script.

1
2
3
4
5
./Producer.py 1 &
./Producer.py 2 &
./Producer.py 3 &

./Receiver.py

If you use something like HTOP or the RabbitMQ admin panel, you can see the messages come and go and monitor the machine status. In my testing, it will uses all the CPU cores available and run them at 100%.

This post is licensed under CC BY 4.0 by the author.