How to close APDL instance with PyMAPDL if something goes wrong...

I am developing a PyMAPDL script that has mistakes in it (it's a work in progress!) and when I run it, it keeps crashing out before the code can reach mapdl.exit()
. This means that my spun-up MAPDL instance remains hanging and I have to kill it manually via task manager.
I want to write a function using atexit that can exit MAPDL sensibly. However, I may not have access to the mapdl
object.
I am running the code in a script like the following:
try: func() except Exception as error: print(Exception(error)) raise
In this case func()
contains the mapdl
object generated from launch_mapdl()
. Can I get it using any PyMAPDL commands and close any hanging MAPDL instances on exit?
Comments
-
Hi James,
Thank you for raising this question.
The easiest solution I have in mind would be to use the PyMAPDL CLI
mapdl stop
, ormapdl stop --all
if you have multiple opened instances, from your terminal.You can find documentation about it at the following link.
Let me know if it solves your issue or if you have any constraints that prevent you from using the CLI.
0 -
the CLI is a good way to do it broadly, but I'd like to do it within the code as I'm actually running tests on PyMAPDL code that I can't change (they're being dynamically generated, by other scripts).
And there are upwards of 25 scripts, each being tested 2-3 times, which means that I'm going to end up with nearly 100 instances of MAPDL running at once if I'm not careful. It would be better to have something I can put in the code to kill the sessions as I go.
1