From d052a481e499bd778f9e300261a62e1e9a2b24b4 Mon Sep 17 00:00:00 2001 From: Mainak Kundu Date: Thu, 21 Nov 2024 22:08:02 -0500 Subject: [PATCH] feat: Add a hidden way to access Fluent's stdout/stderr --- src/ansys/fluent/core/__init__.py | 8 ++++++++ src/ansys/fluent/core/launcher/launcher_utils.py | 4 ++++ src/ansys/fluent/core/launcher/standalone_launcher.py | 11 ++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ansys/fluent/core/__init__.py b/src/ansys/fluent/core/__init__.py index 3ddace72dba..61d4651a2d2 100644 --- a/src/ansys/fluent/core/__init__.py +++ b/src/ansys/fluent/core/__init__.py @@ -133,3 +133,11 @@ def version_info() -> str: # Whether to clear environment variables related to Fluent parallel mode CLEAR_FLUENT_PARA_ENVS = False + +# Set stdout of the launched Fluent process +# Valid values are same as subprocess.Popen's stdout argument +LAUNCH_FLUENT_STDOUT = None + +# Set stderr of the launched Fluent process +# Valid values are same as subprocess.Popen's stderr argument +LAUNCH_FLUENT_STDERR = None diff --git a/src/ansys/fluent/core/launcher/launcher_utils.py b/src/ansys/fluent/core/launcher/launcher_utils.py index 16451d7283e..a003896a8a0 100644 --- a/src/ansys/fluent/core/launcher/launcher_utils.py +++ b/src/ansys/fluent/core/launcher/launcher_utils.py @@ -28,6 +28,10 @@ def _get_subprocess_kwargs_for_fluent(env: Dict[str, Any], argvals) -> Dict[str, kwargs: Dict[str, Any] = {} if is_slurm: kwargs.update(stdout=subprocess.PIPE) + else: + kwargs.update( + stdout=pyfluent.LAUNCH_FLUENT_STDOUT, stderr=pyfluent.LAUNCH_FLUENT_STDERR + ) if is_windows(): kwargs.update(shell=True, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) else: diff --git a/src/ansys/fluent/core/launcher/standalone_launcher.py b/src/ansys/fluent/core/launcher/standalone_launcher.py index 1e807bab7dc..db0e384191e 100644 --- a/src/ansys/fluent/core/launcher/standalone_launcher.py +++ b/src/ansys/fluent/core/launcher/standalone_launcher.py @@ -177,6 +177,8 @@ def __init__( The allocated machines and core counts are queried from the scheduler environment and passed to Fluent. """ + import ansys.fluent.core as pyfluent + self.argvals, self.new_session = _get_argvals_and_session(locals().copy()) self.file_transfer_service = file_transfer_service if os.getenv("PYFLUENT_SHOW_SERVER_GUI") == "1": @@ -214,7 +216,9 @@ def __init__( self.argvals["topy"], self.argvals["journal_file_names"] ) - if is_windows(): + if is_windows() and not ( + pyfluent.LAUNCH_FLUENT_STDOUT or pyfluent.LAUNCH_FLUENT_STDERR + ): # Using 'start.exe' is better; otherwise Fluent is more susceptible to bad termination attempts. self._launch_cmd = 'start "" ' + self._launch_string else: @@ -231,7 +235,7 @@ def __call__(self): try: logger.debug(f"Launching Fluent with command: {self._launch_cmd}") - subprocess.Popen(self._launch_cmd, **self._kwargs) + process = subprocess.Popen(self._launch_cmd, **self._kwargs) try: _await_fluent_launch( @@ -247,7 +251,7 @@ def __call__(self): logger.warning( f"Retrying Fluent launch with less robust command: {launch_cmd}" ) - subprocess.Popen(launch_cmd, **self._kwargs) + process = subprocess.Popen(launch_cmd, **self._kwargs) _await_fluent_launch( self._server_info_file_name, self.argvals["start_timeout"], @@ -264,6 +268,7 @@ def __call__(self): launcher_args=self.argvals, inside_container=False, ) + session._process = process start_watchdog = _confirm_watchdog_start( self.argvals["start_watchdog"], self.argvals["cleanup_on_exit"],