Created
July 13, 2013 11:11
-
-
Save reiki4040/5990366 to your computer and use it in GitHub Desktop.
example for Amazon 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
# -*- coding:utf-8 -*- | |
import time | |
import sys | |
import boto.sqs | |
from boto.sqs.message import Message | |
# tokyo region | |
DEFAULT_REGION = 'ap-northeast-1' | |
Q_NAME = 'myqueue' | |
def push(): | |
conn = boto.sqs.connect_to_region(DEFAULT_REGION) | |
q = conn.create_queue(Q_NAME) | |
msg = Message() | |
while True: | |
body = 'Try message queuing! at ' + time.strftime('%Y/%m/%d-%H:%M:%S') | |
msg.set_body(body) | |
written_msg = q.write(msg) | |
print('push to SQS: ' + body) | |
time.sleep(3) | |
def pull(): | |
conn = boto.sqs.connect_to_region(DEFAULT_REGION) | |
q = conn.create_queue(Q_NAME) | |
while True: | |
receive_msgs = q.get_messages(10) | |
print('receive messages:' + str(len(receive_msgs))) | |
for rec_msg in receive_msgs: | |
print(time.strftime('%Y/%M/%d-%H%m%S') + rec_msg.get_body()) | |
q.delete_message(rec_msg) | |
time.sleep(5) | |
def main(): | |
argvs = sys.argv | |
if len(argvs) < 2: | |
print 'please specify argument push or pull.' | |
sys.exit() | |
if argvs[1] == 'push': | |
push() | |
elif argvs[1] == 'pull': | |
pull() | |
else: | |
print 'please specify argument push or pull.' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment