Skip to content

Catching MAPDL process output #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/api/logging.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _api_logging:

Logging
=======
To make the logging of events consistent, PyMAPDL has a specific
Expand Down
8 changes: 4 additions & 4 deletions examples/00-mapdl-examples/composite_dcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,18 +308,18 @@
# Use PyDPF to visualize the crack opening throughout the simulation as
# an animation.

temp_directory = tempfile.gettempdir()
rst_path = mapdl.download_result(temp_directory)

try:
# ONLY IF DPF SERVER DEPLOYED WITH gRPC COMMUNICATION
# Upload file to DPF server
temp_directory = tempfile.gettempdir()
rst_path = mapdl.download_result(temp_directory)
dpf.connect_to_server()
server_file_path = dpf.upload_file_in_tmp_folder(rst_path)
data_src = dpf.DataSources(server_file_path)
except:
# Using DPF locally
rst = mapdl.download_result()
data_src = dpf.DataSources(rst)
data_src = dpf.DataSources(rst_path)

# Generate the DPF model
model = Model(data_src)
Expand Down
69 changes: 49 additions & 20 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def launch_grpc(
additional_switches="",
override=True,
timeout=20,
verbose=False,
verbose=None,
add_env_vars=None,
replace_env_vars=None,
**kwargs,
Expand Down Expand Up @@ -268,6 +268,10 @@ def launch_grpc(
recommended unless debugging the MAPDL start. Default
``False``.

.. deprecated:: v0.65.0
The ``verbose`` argument is deprecated and will be removed in a future release.
Use a logger instead. See :ref:`api_logging` for more details.

kwargs : dict
Not used. Added to keep compatibility between Mapdl_grpc and
launcher_grpc ``start_parm``s.
Expand Down Expand Up @@ -399,6 +403,15 @@ def launch_grpc(
# disable all MAPDL pop-up errors:
os.environ["ANS_CMD_NODIAG"] = "TRUE"

if verbose is not None:
warnings.warn(
"The ``verbose`` argument is deprecated and will be removed in a future release. "
"Use a logger instead. See :ref:`api_logging` for more details.",
DeprecationWarning,
)
elif verbose is None:
verbose = False

# use temporary directory if run_location is unspecified
if run_location is None:
run_location = create_temp_dir()
Expand Down Expand Up @@ -505,20 +518,19 @@ def launch_grpc(

LOG.info(f"Running in {ip}:{port} the following command: '{command}'")

if verbose: # pragma: no cover
subprocess.Popen(command, shell=os.name != "nt", cwd=run_location, env=env_vars)

else:
subprocess.Popen(
command,
shell=os.name != "nt",
cwd=run_location,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=env_vars,
)
LOG.debug("MAPDL started in background.")
if verbose:
print(command)

process = subprocess.Popen(
command,
shell=os.name != "nt",
cwd=run_location,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env_vars,
)
LOG.debug("MAPDL started in background.")

# watch for the creation of temporary files at the run_directory.
# This lets us know that the MAPDL process has at least started
Expand All @@ -539,7 +551,7 @@ def launch_grpc(
f"MAPDL failed to start (No err file generated in '{run_location}')"
)

return port, run_location
return port, run_location, process


def launch_remote_mapdl(
Expand Down Expand Up @@ -1225,7 +1237,7 @@ def launch_mapdl(
log_apdl=None,
remove_temp_files=None,
remove_temp_dir_on_exit=False,
verbose_mapdl=False,
verbose_mapdl=None,
license_server_check=True,
license_type=None,
print_com=False,
Expand Down Expand Up @@ -1373,6 +1385,10 @@ def launch_mapdl(
MAPDL. This should be used for debugging only as output can
be tracked within pymapdl. Default ``False``.

.. deprecated:: v0.65.0
The ``verbose_mapdl`` argument is deprecated and will be removed in a future release.
Use a logger instead. See :ref:`api_logging` for more details.

license_server_check : bool, optional
Check if the license server is available if MAPDL fails to
start. Only available on ``mode='grpc'``. Defaults ``True``.
Expand Down Expand Up @@ -1594,6 +1610,14 @@ def launch_mapdl(
remove_temp_dir_on_exit = remove_temp_files
remove_temp_files = None

if verbose_mapdl is not None:
warnings.warn(
"The ``verbose_mapdl`` argument is deprecated and will be removed in a future release. "
"Use a logger instead. See :ref:`api_logging` for more details.",
DeprecationWarning,
)
verbose_mapdl = False

# These parameters are partially used for unit testing
set_no_abort = kwargs.get("set_no_abort", True)

Expand Down Expand Up @@ -1788,7 +1812,7 @@ def launch_mapdl(
# configure timeout to be 90% of the wait time of the startup
# time for Ansys.
LOG.debug("Checking license server.")
lic_check = LicenseChecker(timeout=start_timeout * 0.9)
lic_check = LicenseChecker(timeout=int(start_timeout * 0.9))
lic_check.start()

try:
Expand Down Expand Up @@ -1816,12 +1840,12 @@ def launch_mapdl(
**start_parm,
)
elif mode == "grpc":
port, actual_run_location = launch_grpc(
port, actual_run_location, process = launch_grpc(
port=port,
verbose=verbose_mapdl,
ip=ip,
add_env_vars=add_env_vars,
replace_env_vars=replace_env_vars,
verbose=verbose_mapdl,
**start_parm,
)
mapdl = MapdlGrpc(
Expand All @@ -1832,10 +1856,12 @@ def launch_mapdl(
set_no_abort=set_no_abort,
remove_temp_dir_on_exit=remove_temp_dir_on_exit,
log_apdl=log_apdl,
process=process,
**start_parm,
)
if run_location is None:
mapdl._path = actual_run_location

except Exception as exception:
# Failed to launch for some reason. Check if failure was due
# to the license check
Expand All @@ -1850,6 +1876,9 @@ def launch_mapdl(
LOG.debug("Stopping license server check.")
lic_check.is_connected = True

# Setting launched property
mapdl._launched = True

return mapdl


Expand Down
9 changes: 9 additions & 0 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ def __init__(
self._stored_commands = []
self._response = None
self._mode = None
self._mapdl_process = None
self._launched = False
self._stderr = None
self._stdout = None

if _HAS_PYVISTA:
if use_vtk is not None: # pragma: no cover
Expand Down Expand Up @@ -4040,3 +4044,8 @@ def on_docker(self):
def is_local(self):
"""Check if the instance is running locally or remotely."""
return self._local

@property
def launched(self):
"""Check if the MAPDL instance has been launched by PyMAPDL."""
return self._launched
51 changes: 35 additions & 16 deletions src/ansys/mapdl/core/mapdl_corba.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import sys
import time
import warnings
import weakref

from ansys.mapdl.core.errors import MapdlExitedError, MapdlRuntimeError
Expand Down Expand Up @@ -58,7 +59,7 @@ def launch_corba(
run_location=None,
jobname=None,
nproc=None,
verbose=False,
verbose=None,
additional_switches="",
start_timeout=60,
**kwargs, # ignore extra kwargs
Expand All @@ -75,6 +76,13 @@ def launch_corba(
# Using stored parameters so launch command can be run from a
# cached state (when launching the GUI)

if verbose is not None: # pragma: no cover
warnings.warn(
"The ``verbose`` argument is deprecated and will be removed in a future release. "
"Use a logger instead. See :ref:`api_logging` for more details.",
DeprecationWarning,
)

# can't run /BATCH in windows, so we trick it using "-b" and
# provide a dummy input file
if os.name == "nt":
Expand All @@ -98,17 +106,14 @@ def launch_corba(
if os.path.isfile(broadcast_file):
os.remove(broadcast_file)

if verbose:
subprocess.Popen(command, shell=True, cwd=run_location)
else:
subprocess.Popen(
command,
shell=True,
cwd=run_location,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
process = subprocess.Popen(
command,
shell=True,
cwd=run_location,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)

# listen for broadcast file
telapsed = 0
Expand Down Expand Up @@ -138,7 +143,7 @@ def launch_corba(

# return CORBA key
keyfile = os.path.join(run_location, "aaS_MapdlId.txt")
return open(keyfile).read()
return open(keyfile).read(), process


class MapdlCorba(_MapdlCore):
Expand All @@ -157,7 +162,11 @@ class MapdlCorba(_MapdlCore):
additional_switches='', timeout

verbose : bool
Print all output from MAPDL to Python. Useful for debugging
Print all output from MAPDL to Python. Useful for debugging.

.. deprecated:: v0.65.0
The ``verbose_mapdl`` argument is deprecated and will be removed in a future release.
Use a logger instead. See :ref:`api_logging` for more details.

log_file : bool, optional
Copy the log to a file called `logs.log` located where the
Expand All @@ -175,7 +184,7 @@ def __init__(
use_vtk=True,
log_file=True,
log_broadcast=False,
verbose=False,
verbose=None,
**start_parm,
):
"""Open a connection to MAPDL via a CORBA interface"""
Expand All @@ -188,6 +197,15 @@ def __init__(
**start_parm,
)

if verbose is not None:
warnings.warn(
"The ``verbose`` argument is deprecated and will be removed in a future release. "
"Use a logger instead. See :ref:`api_logging` for more details.",
DeprecationWarning,
)
elif verbose is None:
verbose = False

self._mode = "corba"
self._broadcast_logger = None
self._server = None
Expand All @@ -204,8 +222,9 @@ def __init__(

def _launch(self, start_parm, verbose):
"""Launch CORBA."""
corba_key = launch_corba(verbose=verbose, **start_parm)
corba_key, process = launch_corba(verbose=verbose, **start_parm)
self._corba_key = corba_key
self._mapdl_process = process

orb = CORBA.ORB_init()
self._server = orb.string_to_object(corba_key)
Expand Down
Loading