@@ -424,7 +424,7 @@ def __init__(
424424
425425 self ._busy : bool = False # used to check if running a command on the server
426426 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 )
428428 self ._health_response_queue : Optional ["Queue" ] = None
429429 self ._exiting : bool = False
430430 self ._exited : Optional [bool ] = None
@@ -1117,32 +1117,31 @@ def exit(self, save=False, force=False, **kwargs):
11171117
11181118 Notes
11191119 -----
1120+ If Mapdl didn't start the instance, then this will be ignored unless
1121+ ``force=True``.
1122+
11201123 If ``PYMAPDL_START_INSTANCE`` is set to ``False`` (generally set in
11211124 remote testing or documentation build), then this will be
11221125 ignored. Override this behavior with ``force=True`` to always force
11231126 exiting MAPDL regardless of your local environment.
11241127
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+
11251131 Examples
11261132 --------
11271133 >>> mapdl.exit()
11281134 """
11291135 # check if permitted to start (and hence exit) instances
11301136 from ansys .mapdl import core as pymapdl
11311137
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+ )
11361141
11371142 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 :
11461145 # Already exited.
11471146 self ._log .debug ("Already exited" )
11481147 return
@@ -1153,26 +1152,25 @@ def exit(self, save=False, force=False, **kwargs):
11531152
11541153 if not force :
11551154 # 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+ )
11581159 return
11591160
11601161 # or building the gallery
11611162 if pymapdl .BUILDING_GALLERY :
11621163 self ._log .info ("Ignoring exit due as BUILDING_GALLERY=True" )
11631164 return
11641165
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
11701170
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." )
11761174
11771175 # Exiting remote instances
11781176 if self ._remote_instance : # pragma: no cover
@@ -3818,20 +3816,17 @@ def __del__(self):
38183816 """In case the object is deleted"""
38193817 # We are just going to escape early if needed, and kill the HPC job.
38203818 # 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
38293821
3830- self .kill_job (self .jobid )
3822+ if not self ._start_instance :
3823+ # Early skip if start_instance is False
3824+ return
38313825
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 )
38343829
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