Skip to content

Commit c82a4df

Browse files
committed
Refactor BusinessOperation and Common methods for performance improvements; update changelog accordingly
1 parent 4673fa4 commit c82a4df

File tree

3 files changed

+51
-33
lines changed

3 files changed

+51
-33
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [unreleased] - Unreleased
9+
### Changed
10+
- Improve module loading and initialization for better performance in BusinessProcess by checking if the module is already loaded
11+
- Change OnMessage to MessageHandler in BusinessOperation for better performance and clarity
812

913
## [3.5.0] - 2025-06-30
1014
### Added

src/iop/cls/IOP/BusinessOperation.cls

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Class IOP.BusinessOperation Extends (Ens.BusinessOperation, IOP.Common) [ Inheri
77

88
Parameter SETTINGS = "%classname:Python BusinessOperation,%module:Python BusinessOperation,%settings:Python BusinessOperation,%classpaths:Python BusinessOperation";
99

10-
Method OnMessage(
11-
request As %Library.Persistent,
12-
Output response As %Library.Persistent) As %Status
10+
Method MessageHandler(
11+
pRequest As %Library.Persistent,
12+
Output pResponse As %Library.Persistent) As %Status
1313
{
1414
set tSC = $$$OK
1515
try {
16-
set response = ..%class."_dispatch_on_message"(request)
16+
set pResponse = ..%class."_dispatch_on_message"(pRequest)
1717
} catch ex {
1818
set tSC = ..DisplayTraceback(ex)
1919
}

src/iop/cls/IOP/Common.cls

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ Method OnInit() As %Status
104104
set tSC = $$$OK
105105

106106
try {
107-
LOCK +^PythonSettings:5
108107

109108
if ..%Venv {
110109
$$$ThrowOnError(##class(IOP.Utils).SetPythonSettings(..%PythonRuntimeLibrary, ..%PythonPath, ..%PythonRuntimeLibraryVersion))
@@ -115,7 +114,6 @@ Method OnInit() As %Status
115114
do $system.Python.Debugging(..%traceback)
116115

117116
$$$ThrowOnError(..Connect())
118-
LOCK -^PythonSettings
119117

120118
do ..%class."_debugpy"($this)
121119

@@ -124,7 +122,7 @@ Method OnInit() As %Status
124122

125123
set tSC = ..DisplayTraceback(ex)
126124
}
127-
LOCK -^PythonSettings
125+
128126
quit tSC
129127
}
130128

@@ -147,18 +145,19 @@ ClassMethod SetPythonPath(pClasspaths)
147145
{
148146
set sys = ##class(%SYS.Python).Import("sys")
149147

150-
for i=0:1:(sys.path."__len__"()-1) {
151-
Try {
152-
if sys.path."__getitem__"(i) = pClasspaths {
153-
do sys.path."__delitem__"(i)
154-
}
155-
}
156-
Catch ex {
157-
// do nothing
148+
// Add the classpaths to the Python sys.path only if they are not already present
149+
// Check if the path is already in sys.path
150+
set found = 0
151+
for j=1:1:sys.path."__len__"() {
152+
if sys.path."__getitem__"(j-1) = pClasspaths {
153+
set found = 1
154+
quit
158155
}
159-
160156
}
161-
do sys.path.insert(0, pClasspaths)
157+
if found = 0 {
158+
// If not found, add to sys.path
159+
do sys.path."append"(pClasspaths)
160+
}
162161
}
163162

164163
Method Connect() As %Status
@@ -167,24 +166,39 @@ Method Connect() As %Status
167166
try {
168167

169168
set container = $this
170-
171-
//set classpass
172-
if ..%classpaths '="" {
173-
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
174-
set extraClasspaths = $tr(container.%classpaths,delimiter,"|")
175-
for i=1:1:$l(extraClasspaths,"|") {
176-
set onePath = $p(extraClasspaths,"|",i)
177-
set onePath = ##class(%File).NormalizeDirectory(onePath)
178-
do ..SetPythonPath(onePath)
179-
}
180-
}
181-
if $isObject(..%class)=0 {
182-
set importlib = ##class(%SYS.Python).Import("importlib")
169+
170+
// Before anything, try to check if the module is already present in the sys.modules
171+
set sys = ##class(%SYS.Python).Import("sys")
172+
set moduleName = ..%module
173+
set module = sys.modules.get(moduleName, $$$NULLOREF)
174+
if $isObject(module) {
175+
$$$LOGINFO("Module "_moduleName_" is already imported in sys.modules")
176+
// If the module is already present, we can skip the import
183177
set builtins = ##class(%SYS.Python).Import("builtins")
184-
set module = importlib."import_module"(..%module)
185178
set class = builtins.getattr(module, ..%classname)
186179
set ..%class = class."__new__"(class)
187180
}
181+
else {
182+
183+
//set classpass
184+
if ..%classpaths '="" {
185+
set delimiter = $s($system.Version.GetOS()="Windows":";",1:":")
186+
set extraClasspaths = $tr(container.%classpaths,delimiter,"|")
187+
for i=1:1:$l(extraClasspaths,"|") {
188+
set onePath = $p(extraClasspaths,"|",i)
189+
set onePath = ##class(%File).NormalizeDirectory(onePath)
190+
do ..SetPythonPath(onePath)
191+
}
192+
}
193+
194+
if $isObject(..%class)=0 {
195+
set builtins = ##class(%SYS.Python).Import("builtins")
196+
set module = ##class(%SYS.Python).Import(..%module)
197+
set class = builtins.getattr(module, ..%classname)
198+
set ..%class = class."__new__"(class)
199+
}
200+
}
201+
188202
;
189203
if ..%Extends("IOP.InboundAdapter") || ..%Extends("IOP.OutboundAdapter") {
190204
do ..%class."_set_iris_handles"($this,..BusinessHost)
@@ -352,9 +366,9 @@ ClassMethod OnGetConnections(
352366
if onePath'="" do sys.path.append(onePath)
353367
}
354368
}
355-
set importlib = ##class(%SYS.Python).Import("importlib")
369+
356370
set builtins = ##class(%SYS.Python).Import("builtins")
357-
set module = importlib."import_module"(tModule)
371+
set module = ##class(%SYS.Python).Import(tModule)
358372
set class = builtins.getattr(module, tClassname)
359373
set tClass = class."__new__"(class)
360374

0 commit comments

Comments
 (0)