public
Description: Python wrapper for amqplib to emulate SQS semantics
Homepage: http://notes.variogr.am/
Clone URL: git://github.com/bwhitman/py-amqplib-wrapper.git
py-amqplib-wrapper / amqp-wrapper.py
100644 82 lines (67 sloc) 3.282 kb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import simplejson
import amqplib.client_0_8 as amqp
        
class Queue():
    """Base class for AMQP queue service."""
    def __init__(self, queue_name):
        self.conn = amqp.Connection('HOST_HERE', userid='guest', password='guest', ssl=False)
        self.queue_name = queue_name
        self.ch = self.conn.channel()
 
    def declare(self):
        return self.ch.queue_declare(self.queue_name, passive=False, durable=True, exclusive=False, auto_delete=False)
        
    def __len__(self):
        """Return number of messages waiting in this queue"""
        _,n_msgs,_ = self.declare()
        return n_msgs
    
    def consumers(self):
        """Return how many clients are currently listening to this queue."""
        _,_,n_consumers = self.declare()
        return n_consumers
    
 
class QueueProducer(Queue):
    def __init__(self, queue_name):
        """Create new queue producer (guy that creates messages.)
Will create a queue if the given name does not already exist."""
        Queue.__init__(self, queue_name)
        self.ch.access_request('/data',active=True,read=False,write=True)
        self.ch.exchange_declare('sqs_exchange', 'direct', durable=True, auto_delete=False)
        qname, n_msgs, n_consumers = self.declare()
        print "Connected to %s (%d msgs, %d consumers)" % (qname, n_msgs, n_consumers)
        self.ch.queue_bind(self.queue_name, 'sqs_exchange', self.queue_name)
    
    def delete(self):
        """Delete a queue and closes the queue connection."""
        self.ch.queue_delete(self.queue_name)
        self.ch.close()
        
    def write(self, message):
        """Write a single message to the queue. Message can be a dict or a list or whatever."""
        m = amqp.Message(simplejson.dumps(message), content_type='text/x-json')
        self.ch.basic_publish(m, 'sqs_exchange', self.queue_name)
 
class QueueConsumer(Queue):
    def __init__(self, queue_name):
        """Create new queue consumer (guy that listens to messages.)"""
        Queue.__init__(self, queue_name)
        self.ch.access_request('/data',active=True, read=True, write=False)
        self.ch.queue_bind(self.queue_name, 'sqs_exchange', self.queue_name)
    
    def ack(self, delivery_tag):
        """Acknowledge receipt of the message (which will remove it off the queue.)
Do this after you've completed your processing of the message.
Otherwise after some amount of time (about a minute) it will go back on the queue.
e.g.
(object, tag) = consumer.get()
if(object is not None):
error = doSomethingWithMessage(object)
if(error is None):
consumer.ack(tag)
"""
        self.ch.basic_ack(delivery_tag)
        
    def get(self):
        """Get a message. Returns the object and a delivery tag."""
        m = self.ch.basic_get(self.queue_name)
        if(m is not None):
            try:
                ret = simplejson.loads(m.body)
            except ValueError:
                print "Problem decoding json for body " + str(m.body) + ". deleting."
                self.ack(m.delivery_tag)
                return (None, None)
            return (ret, m.delivery_tag)
        else:
            return (None,None)