RabbitMQ : Hello World
RabbitMQ & Celery Tutorials
Installing RabbitMQ & Celery
Hello World RabbitMQ
Work Queues (Task Queues) : RabbitMQ
Exchanges - Publish/Subscribe : RabbitMQ
Multiple bindings - Routing : RabbitMQ
Queueing Messages using Celery with RabbitMQ Message Broker Server
We're going to make two programs:
- producer for sending a message
- consumer for receiving the message from the queue (we'll name the queue as "hello") and printing it.
AMQP (Advanced Message Queuing Protocol) has been approved for release as an ISO and IEC International Standard.
"AMQP is an open standard application layer protocol for message-oriented middleware. The defining features of AMQP are message orientation, queuing, routing (including point-to-point and publish-and-subscribe), reliability and security." - wiki.
Since RabbitMQ is based on AMQP, to use Rabbit properly, we need a library that understands it. We'll use pika. It is a pure-Python implementation of the AMQP 0-9-1 protocol that tries to stay fairly independent of the underlying network support library.
Since pika installation depends on git-core packages, we may need to install it first:
$ sudo apt-get install python-pip git-core $ sudo pip install pika==0.9.13
We'll write our first program send a single message to the queue (send.py). The first thing we need to do is to establish a connection with RabbitMQ server.
#!/usr/bin/env python import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = connection.channel() channel.queue_declare(queue='hello')
We're now connected to a broker on the localhost. Since RabbitMQ will just trash the message that's sent to non-existing location, we need to make sure the recipient queue exists. So, we created a queue to which the message will be delivered, and named it "hello". At this point, we're now ready to send a message to our queue. The message will just contain a string "Hello World!".
In RabbitMQ, a message can never be sent directly to the queue, it always needs to go through an exchange as shown in the picture below.
Picture from slides.com.
We're going to use default exchange identified by an empty string though it allows us to specify exactly to which queue the message should go. The queue name needs to be specified in the routing_key parameter:
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print " [x] Sent 'Hello World!'"
Before exiting the program we need to make sure the network buffers were flushed and our message was actually delivered to RabbitMQ by closing the connection.
connection.close()
Here is the final form of send.py:
#!/usr/bin/env python import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = connection.channel() channel.queue_declare(queue='hello') channel.basic_publish(exchange='', routing_key='hello', body='Hello World!') print " [x] Sent 'Hello World!'"
Output:
$ python send.py [x] Sent 'Hello World!'
Our receiving program receive.py will receive messages from the queue and print them on the screen. However, receiving messages from the queue is more complex than sending it. To make it work we need to subscribe a callback function to a queue. This callback function is called by the Pika library when we receive the message, and this callback function will print received message to the screen.
Our receive.py looks like this:
#!/usr/bin/env python import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello') print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback, queue='hello', no_ack=True) channel.start_consuming()
The channel.basic_consume() allows us to tell RabbitMQ that this particular callback function should receive messages from our hello queue.
Note that we have a never-ending loop that waits for data and runs callbacks whenever necessary:
print ' [*] Waiting for messages. To exit press CTRL+C' ... channel.start_consuming()
If we run the receive.py, we get the following output:
$ python receive.py [*] Waiting for messages. To exit press CTRL+C [x] Received 'Hello World!'
While the send.py will stop after every run, the receive.py program doesn't exit. It will stay ready to receive further messages, and may be interrupted with Ctrl-C.
We can check what queues RabbitMQ has and how many messages are in them:
$ sudo rabbitmqctl list_queues Listing queues ... hello 0 ...done.
RabbitMQ & Celery Tutorials
Installing RabbitMQ & Celery
Hello World RabbitMQ
Work Queues (Task Queues) : RabbitMQ
Exchanges - Publish/Subscribe : RabbitMQ
Multiple bindings - Routing : RabbitMQ
Queueing Messages using Celery with RabbitMQ Message Broker Server
Python tutorial
Python Home
Introduction
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Subprocess Module
Regular Expressions with Python
Regular Expressions Cheat Sheet
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
Functions lambda
Built-in Functions
map, filter, and reduce
Decorators
List Comprehension
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
generator.send() method
Iterators
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
argparse
Exceptions
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Closure
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Multithreading ...
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Coding Questions I
Python Coding Questions II
Python Coding Questions III
Python Coding Questions IV
Python Coding Questions V
Python Coding Questions VI
Python Coding Questions VII
Python Coding Questions VIII
Python Coding Questions IX
Python Coding Questions X
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Matplotlib
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Celluar Automata
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization