(Python 3)
I am using a Python generator to read messages from a queue.
After the consumer reads the queue message, he should be able to tell the generator to delete the queue message if it was successfully processed.
In order to pass .send () to the Python generator, it seems that I have to pass first. (No) to the generator. This makes my code thicker than I think it should be.
Can someone suggest a way for qconsumer.py to control a generator with fewer lines of code? I have identified which lines below, I hope to eliminate.
In short, how can I make the code below more compact, any suggestions on how I can delete lines?
The following is the qconsumer.py code:
from qserver import Qserver
myqserver = Qserver()
myproducer = myqserver.producer()
myproducer.send(None)
for msg in myproducer:
print(msg)
if messageprocessok:
myproducer.send('delete')
The following is the qserver.py code:
import boto
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
QNAME = 'qinbound'
SQSREGION = 'us-west-1'
class Qserver():
"""A simple Q server."""
def __init__(self, qname=None, sqsregion=None):
self.qname = qname or QNAME
self.sqsregion = sqsregion or SQSREGION
self.sqsconn = boto.sqs.connect_to_region(self.sqsregion)
self.q_in = self.sqsconn.get_queue(self.qname)
def producer(self):
while True:
qmessage = self.q_in.read(wait_time_seconds=20)
if qmessage is None:
continue
action = (yield qmessage.get_body())
if action == 'delete':
self.q_in.delete_message(qmessage)