RabbitMQ is a powerful messaging broker based on the Advanced Message Queueing Protocol (AMQP). Thanks to the neutral nature of the AMQP spec, it is easy to connect to it from many platforms, including Python. In this blog entry, we will:
- Create a simple stock ticker Python application
- Create a brokerage Python application that decides when to buy and sell.
- Compare pika, an AMQP library created by the RabbitMQ team, with py-amqplib.
You can find all the source code for this blog at
http://github.com/gregturn/amqp-demo. This assumes you have already installed RabbitMQ based on
instructions for your platform and fired it up. Personally, I have it running on my Mac OS X machine (snow leopard).
By the way:
The code written in this blog entry is for demonstration purposes only. Do not rely on the algorithms for financial advice.
With that out of the way, let's write some code!
Building the stock ticker
A good example for a messaging solution is a stock ticker system. The stock exchange publishes messages to the broker indicating stock name, price, and time.
import pickle
import random
import time
class Ticker(object):
def __init__(self, publisher, qname):
self.publisher = publisher
# This quickly creates four random stock symbols
chars = range(ord("A"), ord("Z")+1)
def random_letter(): return chr(random.choice(chars))
self.stock_symbols = [random_letter…