Introduction to Computational Analysis




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Cloud server

Linode

Rackspace

Amazon EC2

WebFaction

Message queue

rabbitmq

service rabbitmq-server start

zeromq

gevent

gingko

Message producer

from pika import SelectConnection, ConnectionParameters
from time import sleep

CHANNEL = None
QUEUE = 'xxx'

def on_connection_open(connection):
    connection.channel(on_channel_open)

def on_channel_open(channel):
    global CHANNEL
    CHANNEL = channel
    CHANNEL.queue_declare(queue=QUEUE, callback=on_queue_declared)

def on_queue_declared(frame):
    count = 0
    while True:
        body = str(count)
        CHANNEL.basic_publish(exchange='', routing_key=QUEUE, body=body)
        print 'Sent %s' % body
        sleep(3)
        count += 1

parameters = ConnectionParameters()
connection = SelectConnection(parameters, on_open_callback=on_connection_open)
try:
    connection.ioloop.start()
except KeyboardInterrupt:
    connection.close()
    connection.ioloop.start()

Message consumer

from pika import SelectConnection, ConnectionParameters

CHANNEL = None
QUEUE = 'xxx'

def on_connection_open(connection):
    connection.channel(on_channel_open)

def on_channel_open(channel):
    global CHANNEL
    CHANNEL = channel
    CHANNEL.queue_declare(queue=QUEUE, callback=on_queue_declared)

def on_queue_declared(frame):
    CHANNEL.basic_consume(queue=QUEUE, consumer_callback=consume)

def consume(channel, method, header, body):
    CHANNEL.basic_ack(delivery_tag=method.delivery_tag)
    print 'Received %s' % body

parameters = ConnectionParameters()
connection = SelectConnection(parameters, on_open_callback=on_connection_open)
try:
    connection.ioloop.start()
except KeyboardInterrupt:
    connection.close()
    connection.ioloop.start()