-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_msgs.py
78 lines (61 loc) · 1.81 KB
/
send_msgs.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
67
68
69
70
71
72
73
74
75
76
77
78
from azure.servicebus import ServiceBusService, Message, Queue
import multiprocessing
import datetime
import json
#import sys
#script starts 7 processes to send messages to populate service bus queue
#run from local machine
#script is bound on the network operation of sending the messages, therefore output is slow
#to remedy the above issue I included a .bat file to run 6 instances of this script which should work
def send_messages(process_name):
#create service bus service object
bus_service = ServiceBusService(
service_namespace='jlm4dde996c8a5680751',
shared_access_key_name='RootManageSharedAccessKey',
shared_access_key_value='ZPvfBo7KrZAf5zG+gs1MS4kSvCbcI4/uSfDDd/+rF3c=')
print("running...")
#create message dictionary
msgobj = {
"TransactionID": "1",
"UserId":"A1",
"SellerID":"S1",
"Product Name":"Financial Trap",
"Sale Price":1000000,
"Transaction Date": datetime.date.today().strftime("%B %d, %Y")
}
#convert message dictionary to json string
msg_json = json.dumps(msgobj)
count = 0
while 1:
try:
#create and send failure message after every 1000 messages
if count == 1000:
msg = Message('Error Message')
bus_service.send_queue_message('jlmtestqueue1', msg)
count = 0
#send the json string as a message
else:
msg = Message(msg_json)
bus_service.send_queue_message('jlmtestqueue1', msg)
count += 1
except KeyboardInterrupt:
print("failure")
return ("X")
#main program
if __name__ == '__main__':
#create process pool
p = multiprocessing.Pool(7)
params = []
#populate an array with process names as parameters
for i in range(7):
params.append("%s: %s" % ("Process", i))
y = "hi"
#map and start processes
try:
y= p.map(send_messages, params)
print(y)
except KeyboardInterrupt:
print(y)
print("ended")
#while 1:
#pass