@@ -424,7 +424,7 @@ def __init__(
424
424
425
425
self ._busy : bool = False # used to check if running a command on the server
426
426
self ._local : bool = start_parm .get ("local" , True )
427
- self ._launched : bool = start_parm .get ("launched" , True )
427
+ self ._launched : bool = start_parm .get ("launched" , False )
428
428
self ._health_response_queue : Optional ["Queue" ] = None
429
429
self ._exiting : bool = False
430
430
self ._exited : Optional [bool ] = None
@@ -1117,32 +1117,31 @@ def exit(self, save=False, force=False, **kwargs):
1117
1117
1118
1118
Notes
1119
1119
-----
1120
+ If Mapdl didn't start the instance, then this will be ignored unless
1121
+ ``force=True``.
1122
+
1120
1123
If ``PYMAPDL_START_INSTANCE`` is set to ``False`` (generally set in
1121
1124
remote testing or documentation build), then this will be
1122
1125
ignored. Override this behavior with ``force=True`` to always force
1123
1126
exiting MAPDL regardless of your local environment.
1124
1127
1128
+ If ``Mapdl.finish_job_on_exit`` is set to ``True`` and there is a valid
1129
+ JobID in ``Mapdl.jobid``, then the SLURM job will be canceled.
1130
+
1125
1131
Examples
1126
1132
--------
1127
1133
>>> mapdl.exit()
1128
1134
"""
1129
1135
# check if permitted to start (and hence exit) instances
1130
1136
from ansys .mapdl import core as pymapdl
1131
1137
1132
- if hasattr (self , "_log" ):
1133
- self ._log .debug (
1134
- f"Exiting MAPLD gRPC instance { self .ip } :{ self .port } on '{ self ._path } '."
1135
- )
1138
+ self ._log .debug (
1139
+ f"Exiting MAPLD gRPC instance { self .ip } :{ self .port } on '{ self ._path } '."
1140
+ )
1136
1141
1137
1142
mapdl_path = self ._path # using cached version
1138
- if self ._exited is None :
1139
- self ._log .debug ("'self._exited' is none." )
1140
- return # Some edge cases the class object is not completely
1141
- # initialized but the __del__ method
1142
- # is called when exiting python. So, early exit here instead an
1143
- # error in the following self.directory command.
1144
- # See issue #1796
1145
- elif self ._exited :
1143
+
1144
+ if self ._exited :
1146
1145
# Already exited.
1147
1146
self ._log .debug ("Already exited" )
1148
1147
return
@@ -1153,26 +1152,25 @@ def exit(self, save=False, force=False, **kwargs):
1153
1152
1154
1153
if not force :
1155
1154
# ignore this method if PYMAPDL_START_INSTANCE=False
1156
- if not self ._start_instance :
1157
- self ._log .info ("Ignoring exit due to PYMAPDL_START_INSTANCE=False" )
1155
+ if not self ._start_instance or not self ._launched :
1156
+ self ._log .info (
1157
+ "Ignoring exit due to PYMAPDL_START_INSTANCE=False or because PyMAPDL didn't launch the instance."
1158
+ )
1158
1159
return
1159
1160
1160
1161
# or building the gallery
1161
1162
if pymapdl .BUILDING_GALLERY :
1162
1163
self ._log .info ("Ignoring exit due as BUILDING_GALLERY=True" )
1163
1164
return
1164
1165
1165
- # Actually exiting MAPDL instance
1166
- if self .finish_job_on_exit :
1167
- self ._exiting = True
1168
- self ._exit_mapdl (path = mapdl_path )
1169
- self ._exited = True
1166
+ # Exiting MAPDL instance if we launched.
1167
+ self ._exiting = True
1168
+ self ._exit_mapdl (path = mapdl_path )
1169
+ self ._exited = True
1170
1170
1171
- # Exiting HPC job
1172
- if self ._mapdl_on_hpc :
1173
- self .kill_job (self .jobid )
1174
- if hasattr (self , "_log" ):
1175
- self ._log .debug (f"Job (id: { self .jobid } ) has been cancel." )
1171
+ if self .finish_job_on_exit and self ._mapdl_on_hpc :
1172
+ self .kill_job (self .jobid )
1173
+ self ._log .debug (f"Job (id: { self .jobid } ) has been cancel." )
1176
1174
1177
1175
# Exiting remote instances
1178
1176
if self ._remote_instance : # pragma: no cover
@@ -3818,20 +3816,17 @@ def __del__(self):
3818
3816
"""In case the object is deleted"""
3819
3817
# We are just going to escape early if needed, and kill the HPC job.
3820
3818
# The garbage collector remove attributes before we can evaluate this.
3821
- try :
3822
- # Exiting HPC job
3823
- if (
3824
- hasattr (self , "_mapdl_on_hpc" )
3825
- and self ._mapdl_on_hpc
3826
- and hasattr (self , "finish_job_on_exit" )
3827
- and self .finish_job_on_exit
3828
- ):
3819
+ if self ._exited :
3820
+ return
3829
3821
3830
- self .kill_job (self .jobid )
3822
+ if not self ._start_instance :
3823
+ # Early skip if start_instance is False
3824
+ return
3831
3825
3832
- if not self ._start_instance :
3833
- return
3826
+ # Killing the instance if we launched it.
3827
+ if self ._launched :
3828
+ self ._exit_mapdl (self ._path )
3834
3829
3835
- except Exception as e : # nosec B110
3836
- # This is on clean up.
3837
- pass # nosec B110
3830
+ # Exiting HPC job
3831
+ if self . _mapdl_on_hpc and self . finish_job_on_exit :
3832
+ self . kill_job ( self . jobid )
0 commit comments