Created
November 1, 2015 14:50
-
-
Save alhopper/0c089b7036759c80c32b to your computer and use it in GitHub Desktop.
python code to get/process events from AWS SQS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from boto import sqs | |
from time import sleep | |
import time | |
import logging | |
import logging.handlers | |
import string | |
import os | |
import sys | |
def init_sqs(log): | |
''' | |
Setup Simple Queuing Service (SQS) on Amazon Web Services | |
''' | |
log.debug('Connecting to SQS') | |
conn = sqs.connect_to_region('us-east-1') | |
q = conn.get_queue('garage-door') | |
return q | |
def process_msgs(q, rs, log): | |
''' | |
Process all messages accumulated in SQS. | |
''' | |
log.debug('Process Messages') | |
# only process the last (most recent) message | |
numMsgs = len(rs) | |
m = rs[numMsgs - 1] | |
process_msg(m, log) | |
delete_all_msgs(q, rs, log) | |
def delete_all_msgs(q, rs, log): | |
''' | |
Delete all messages in the result set | |
''' | |
log.debug('delete all messages') | |
for m in rs: | |
res = q.delete_message(m) | |
if res: | |
log.info('Message deleted') | |
def process_msg(m, log): | |
''' | |
Process an individual SQS message (simplified). | |
''' | |
log.debug('Process Message') | |
if payload.find('some-magic-string') >= 0: | |
os.system('sudo /home/pi/actuategpiopin.py') | |
log.debug('end of process msg') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment