Desktop sessions#
PyAEDT can use Desktop either to start a new AEDT session or to connect to an
existing one. The close_on_exit argument controls whether that AEDT session is
closed when leaving a context manager or when the Python interpreter session ends.
When close_on_exit is not explicitly provided, PyAEDT chooses the behavior automatically:
Inside a context manager (
with Desktop(...)),close_on_exitis treated asTrue.Outside a context manager:
if PyAEDT starts a new AEDT session,
close_on_exitbehaves asTrueif PyAEDT connects to an existing AEDT session,
close_on_exitbehaves asFalse
When close_on_exit is explicitly set to True or False, the user choice is always respected.
Behavior summary#
The following table summarizes the most common behavior combinations:
Usage |
Result on exit |
|---|---|
|
AEDT closes when leaving the context manager |
|
AEDT remains open when leaving the context manager |
|
AEDT closes when the interpreter session ends |
|
PyAEDT connected to an existing session. AEDT remains open when the interpreter session ends |
|
AEDT closes when the interpreter session ends |
|
AEDT remains open when the interpreter session ends |
Use a context manager#
Use a context manager when you want deterministic cleanup at the end of a block:
import ansys.aedt.core
with ansys.aedt.core.Desktop(
version="2026.1",
non_graphical=True,
new_desktop=True,
):
hfss = ansys.aedt.core.Hfss()
maxwell = ansys.aedt.core.Maxwell3d()
# ...
# Work with AEDT here.
# ...
# AEDT is automatically closed here.
If needed, you can still override the default behavior explicitly:
import ansys.aedt.core
with ansys.aedt.core.Desktop(
version="2026.1",
non_graphical=True,
new_desktop=False,
close_on_exit=False,
):
hfss = ansys.aedt.core.Hfss()
maxwell = ansys.aedt.core.Maxwell3d()
# Work with an existing AEDT session here.
# The AEDT session remains open here.
Use Desktop class directly#
When Desktop is used directly, the default behavior depends on whether PyAEDT starts
or attaches to AEDT. You can also release the desktop explicitly for finer control:
import ansys.aedt.core
d = ansys.aedt.core.Desktop(
version="2026.1",
non_graphical=False,
new_desktop=False,
)
hfss = ansys.aedt.core.Hfss()
# Work with AEDT here.
d.release_desktop(close_projects=False, close_desktop=False)
Recommendations#
Use
with Desktop(...)when you want predictable cleanup.Use direct
Desktop(...)construction when you need more manual control over the AEDT session lifecycle.When attaching to an existing AEDT session, consider leaving
close_on_exitunset or setting it explicitly toTrueif you do want PyAEDT to close that session.