@@ -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
164163Method 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