Skip to content

Instantly share code, notes, and snippets.

@GeoffreyPlitt
Created November 22, 2013 20:33
Show Gist options
  • Save GeoffreyPlitt/7606362 to your computer and use it in GitHub Desktop.
Save GeoffreyPlitt/7606362 to your computer and use it in GitHub Desktop.
Example Python code for accessing Amazon SQS
from boto.sqs.connection import SQSConnection
from boto.sqs.message import Message
import time
import simplejson as json
sqs_conn = SQSConnection(ACCESS, SECRET)
def _get_queue_by_name(name):
""" Gets existing queue; or makes one if it doesn't exist
"""
return sqs_conn.create_queue(name)
def write_to_queue(name, body):
q = _get_queue_by_name(name)
m = Message()
m.set_body(body)
q.write(m)
def read_from_queue(name):
""" Returns a tuple of (context, body) if there was anything to process, or (None, None) if not.
Make sure to call mark_as_processed(context) when done.
"""
q = _get_queue_by_name(name)
m = q.read()
if m==None:
return (None, None)
else:
return (m, m.get_body())
def mark_as_processed(name, context):
""" Mark a context (item) as processed so it doesn't get sent through again.
"""
q = _get_queue_by_name(name)
q.delete_message(context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment