-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestSDK.py
More file actions
98 lines (80 loc) · 2.59 KB
/
Copy pathtestSDK.py
File metadata and controls
98 lines (80 loc) · 2.59 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from importlib.machinery import SourceFileLoader
import os
import threading
import pandas as pd
import time
path = os.path.abspath('..') + '/Distributed-Queue/LogQueueSDK/myqueue.py'
sdkSourceFile = SourceFileLoader("myqueue",path).load_module()
def producerTest(fileName):
data = pd.read_csv(fileName, delimiter = '\t', header = None)
data = data.drop(columns = [2,4])
producerTopics = list(set(data[5]))
broker = "localhost:5000"
producer = sdkSourceFile.Producer(broker, producerTopics)
i = 0
# for i in range(data.shape[0]):
while i<data.shape[0]:
time.sleep(1)
row = data.iloc[i]
i += 1
counter = 0
while not producer.can_send():
time.sleep(1)
counter += 1
if counter==60:
break
continue
if counter==60:
break
try:
producer.send(row[5], row[1])
except:
i -= 1
producer.stop()
def consumerTest(topics):
broker = "localhost:5000"
consumer = sdkSourceFile.Consumer(broker, topics)
while True:
counter = 0
while True:
res = consumer.get_next()
if res == -1:
time.sleep(1)
counter += 1
if counter==60:
break
continue
if res is None:
break
print("Consumer Message - {}".format(res))
counter = 0
consumer.stop()
def runProducers():
fileNames = ["TestFiles/test_asgn1/producer_{}.txt".format(i) for i in range(1,6)]
threads = []
topics = ['T-1', 'T-2', 'T-3']
for fileName in fileNames:
tempThread = threading.Thread(target=producerTest, args = (fileName, ))
threads.append(tempThread)
threads[len(threads)-1].start()
for thread in threads:
thread.join()
def runConsumers():
threads = []
topics = [['T-1', 'T-2', 'T-3'], ['T-1', 'T-3'], ['T-1', 'T-3']]
for i in range(3):
tempThread = threading.Thread(target=consumerTest, args = (topics[i], ))
threads.append(tempThread)
threads[len(threads)-1].start()
for thread in threads:
thread.join()
def testSDK():
tProducer = threading.Thread(target=runProducers, args = ())
tProducer.start()
time.sleep(10)
tConsumer = threading.Thread(target=runConsumers, args = ())
tConsumer.start()
tProducer.join()
tConsumer.join()
if __name__ == "__main__":
testSDK()