-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumper.py
67 lines (56 loc) · 1.94 KB
/
dumper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import threading
import Queue
import time
import boto3
import json
import config
# threads config
NO_OF_READ_THREADS = config.NO_OF_READ_THREADS
NO_OF_PROCESS_THREADS = config.NO_OF_PROCESS_THREADS
# sqs config
SQS_CLIENT = boto3.resource('sqs', region_name=config.AWS_REGION)
SQS_QUEUE = SQS_CLIENT.get_queue_by_name(QueueName=config.SQS_NAME)
INTERNAL_QUEUE = Queue.Queue()
# get data from sqs
class SourceData(threading.Thread):
def __init__(self, local_queue):
threading.Thread.__init__(self)
self.local_queue = local_queue
def run(self):
while True:
try:
msgs = SQS_QUEUE.receive_messages(MaxNumberOfMessages=10, WaitTimeSeconds=5)
if len(msgs) == 0:
print "sqs empty"
for msg in msgs:
self.local_queue.put(msg)
msg.delete()
print "new task added to internal queue sz:{}".format(self.local_queue.qsize())
except Exception as err:
print "error:: while receiving data from sqs, {}".format(err)
# process msg
class ProcessMsgs(threading.Thread):
def __init__(self, local_queue):
threading.Thread.__init__(self)
self.local_queue = local_queue
def run(self):
while True:
msg = self.local_queue.get()
try:
if msg != "":
data = json.loads(msg.body)
except Exception as err:
print "error:: parsing, {} ".format(e)
finally:
self.local_queue.task_done()
def main():
for i in range(NO_OF_READ_THREADS):
source_msg_thread = SourceData(INTERNAL_QUEUE)
source_msg_thread.start()
for j in range(NO_OF_PROCESS_THREADS):
parse_thread = ProcessMsgs(INTERNAL_QUEUE)
parse_thread.start()
# wait till all threads processing is completed
INTERNAL_QUEUE.join()
if __name__ == '__main__':
main()