Last active
August 29, 2017 01:43
-
-
Save DingGGu/3b7e51b53ff58f6306cbbb2a1a1b1538 to your computer and use it in GitHub Desktop.
python sqs multithread
This file contains hidden or 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 threading import Thread | |
from Queue import Queue | |
import boto.sqs | |
from boto.sqs.message import RawMessage | |
sqs = boto.sqs.connect_to_region('ap-northeast-1') | |
queue = sqs.get_queue('queue-name') | |
q = Queue() | |
for i in user_id_list: | |
q.put(i) | |
THREADS = 20 | |
def doWork(q): | |
while True: | |
user_id = q.get() | |
if user_id is None: | |
break | |
msg = RawMessage() | |
msg.set_body(json.dumps({ | |
'target': user_id, | |
})) | |
queue.write(msg) | |
for i in range(THREADS): | |
t = Thread(target=doWork, args=(q, )) | |
q.put(None) # for stop while | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment