Skip to content

Commit d2106ff

Browse files
committed
refactor iop code to support multi-sync requests
1 parent eb2f1f9 commit d2106ff

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

demo/python/multi_sync/bo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from iop import BusinessOperation
2+
from msg import MyMessage
3+
4+
import time
5+
6+
class MyMultiBO(BusinessOperation):
7+
def on_message(self, request):
8+
print(f"Received message: {request.message}")
9+
time.sleep(1)
10+
return MyMessage(message=f"Hello, {request.message}")
11+
12+

demo/python/multi_sync/bp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from iop import BusinessProcess
2+
from msg import MyMessage
3+
4+
5+
class MyMultiBP(BusinessProcess):
6+
7+
def on_message(self, request):
8+
msg_one = MyMessage(message="Message1")
9+
msg_two = MyMessage(message="Message2")
10+
11+
tuple_responses = self.send_multi_request_sync([("Python.MyMultiBO", msg_one), ("Python.MyMultiBO", msg_two)])
12+
13+
self.log_info("All requests have been processed")
14+
for target,request,response,status in tuple_responses:
15+
self.log_info(f"Received response: {response.message}")
16+
17+
18+

demo/python/multi_sync/msg.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from iop import Message
2+
from dataclasses import dataclass
3+
4+
@dataclass
5+
class MyMessage(Message):
6+
message : str = None

demo/python/multi_sync/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from bo import MyMultiBO
2+
from bp import MyMultiBP
3+
4+
CLASSES = {
5+
"Python.MyMultiBO": MyMultiBO,
6+
"Python.MyMultiBP": MyMultiBP,
7+
}

src/iop/_business_host.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,39 @@ def send_request_async(self, target, request, description=None):
152152

153153
return self.iris_handle.dispatchSendRequestAsync(target,request,description)
154154

155+
def send_multi_request_sync(self, target_request:list, timeout=-1, description=None)->list:
156+
""" Send the specified list of tuple (target,request) to business process or business operation synchronously.
157+
158+
Parameters:
159+
target_request: a list of tuple (target,request) that specifies the name of the business process or operation to receive the request.
160+
The target is the name of the component as specified in the Item Name property in the production definition, not the class name of the component.
161+
The request is either an instance of a class that is a subclass of Message class or of IRISObject class.
162+
timeout: an optional integer that specifies the number of seconds to wait before treating the send request as a failure. The default value is -1, which means wait forever.
163+
description: an optional string parameter that sets a description property in the message header. The default is None.
164+
Returns:
165+
the list of tuple (target,request,response,status).
166+
"""
167+
# create a list of iris.Ens.CallStructure for each target_request
168+
call_list = []
169+
for target,request in target_request:
170+
call = iris.cls("Ens.CallStructure")._New()
171+
call.TargetDispatchName = target
172+
call.Request = self._dispatch_serializer(request)
173+
call_list.append(call)
174+
# call the dispatchSendMultiRequestSync method
175+
response_list = self.iris_handle.dispatchSendRequestSyncMultiple(call_list,timeout)
176+
# create a list of tuple (target,request,response,status)
177+
result = []
178+
for i in range(len(target_request)):
179+
result.append(
180+
(target_request[i][0],
181+
target_request[i][1],
182+
self._dispatch_deserializer(response_list[i].Response),
183+
response_list[i].ResponseCode
184+
))
185+
return result
186+
187+
155188
def _serialize_pickle_message(self,message):
156189
""" Converts a python dataclass message into an iris iop.message.
157190

src/iop/cls/IOP/Common.cls

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@ Method dispatchSendRequestSync(
149149
quit $g(objResponse)
150150
}
151151

152+
Method dispatchSendRequestSyncMultiple(
153+
pCallStructList As %List,
154+
pTimeout As %Numeric = -1) As %List
155+
{
156+
set builtins = ##class(%SYS.Python).Import("builtins")
157+
// Convert %List to multidimensional array
158+
set tCallStructList=builtins.len(pCallStructList)
159+
$$$LOGINFO("pCallStructList: "_pCallStructList)
160+
for i=0:1:builtins.len(pCallStructList)-1 {
161+
set tCallStructList(i+1) = pCallStructList."__getitem__"(i)
162+
}
163+
164+
$$$LOGINFO("tCallStructList: "_tCallStructList(1))
165+
set tSC = ..SendRequestSyncMultiple(.tCallStructList,pTimeout)
166+
if $$$ISERR(tSC) throw ##class(%Exception.StatusException).CreateFromStatus(tSC)
167+
// Convert multidimensional array to Python list
168+
set tResponseList = builtins.list()
169+
170+
for i=1:1:tCallStructList {
171+
do tResponseList.append(tCallStructList(i))
172+
}
173+
quit tResponseList
174+
}
175+
152176
Method dispatchSendRequestAsync(
153177
target,
154178
request,

0 commit comments

Comments
 (0)