diff --git a/pyproject.toml b/pyproject.toml index 528d299fbe0..bd4aec840b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,7 +51,6 @@ lxml = ">=4.9.2" nltk = ">=3.9.1" numpy= ">=1.14.0,<2.0.0" pandas = ">=1.1.0,<2.3" -psutil = ">=5.9.5" pyyaml = ">=6.0" h5py = { version = "==3.12.1", optional = true } diff --git a/src/ansys/fluent/core/fluent_connection.py b/src/ansys/fluent/core/fluent_connection.py index 5569fb3df6d..38e9f6c47c2 100644 --- a/src/ansys/fluent/core/fluent_connection.py +++ b/src/ansys/fluent/core/fluent_connection.py @@ -2,12 +2,14 @@ from __future__ import annotations +import ctypes from ctypes import c_int, sizeof from dataclasses import dataclass import itertools import logging import os from pathlib import Path +import platform import socket import subprocess import threading @@ -16,7 +18,6 @@ import weakref import grpc -import psutil import ansys.fluent.core as pyfluent from ansys.fluent.core.services import service_creator @@ -292,6 +293,26 @@ def exit_server(self): self.scheme_eval.exec(("(exit-server)",)) +def _pid_exists(pid): + if platform.system() == "Linux": + try: + os.kill(pid, 0) + except OSError: + return False + else: + return True + elif platform.system() == "Windows": + process_query_limited_information = 0x1000 + process_handle = ctypes.windll.kernel32.OpenProcess( + process_query_limited_information, 0, pid + ) + if process_handle == 0: + return False + else: + ctypes.windll.kernel32.CloseHandle(process_handle) + return True + + class FluentConnection: """Encapsulates a Fluent connection. @@ -601,8 +622,8 @@ def wait_process_finished(self, wait: float | int | bool = 60): ) else: _response = timeout_loop( - lambda connection: psutil.pid_exists(connection.fluent_host_pid) - or psutil.pid_exists(connection.cortex_pid), + lambda connection: _pid_exists(connection.fluent_host_pid) + or _pid_exists(connection.cortex_pid), wait, args=(self.connection_properties,), idle_period=0.5, diff --git a/src/ansys/fluent/core/launcher/watchdog_exec b/src/ansys/fluent/core/launcher/watchdog_exec index 24cb64a3d83..d15cdab2134 100644 --- a/src/ansys/fluent/core/launcher/watchdog_exec +++ b/src/ansys/fluent/core/launcher/watchdog_exec @@ -12,11 +12,14 @@ if __name__ == "__main__": import sys import time - import psutil from watchdog import IDLE_PERIOD, WATCHDOG_INIT_FILE import ansys.fluent.core as pyfluent - from ansys.fluent.core.fluent_connection import FluentConnection, get_container + from ansys.fluent.core.fluent_connection import ( + FluentConnection, + _pid_exists, + get_container, + ) from ansys.fluent.core.utils.execution import timeout_exec, timeout_loop watchdog_id = sys.argv[5] @@ -103,7 +106,7 @@ if __name__ == "__main__": def check_pid_down(pid, name): """Check whether pid is down or not.""" - if not psutil.pid_exists(pid): + if not _pid_exists(pid): logger.debug(name + " down") down.append(name) diff --git a/tests/test_fluent_session.py b/tests/test_fluent_session.py index 65cba826c68..1a2e3e596be 100644 --- a/tests/test_fluent_session.py +++ b/tests/test_fluent_session.py @@ -3,12 +3,15 @@ import threading import time -import psutil import pytest import ansys.fluent.core as pyfluent from ansys.fluent.core.examples import download_file -from ansys.fluent.core.fluent_connection import WaitTypeError, get_container +from ansys.fluent.core.fluent_connection import ( + WaitTypeError, + _pid_exists, + get_container, +) from ansys.fluent.core.launcher.error_handler import IpPortNotProvided from ansys.fluent.core.utils.execution import asynchronous, timeout_loop from ansys.fluent.core.utils.fluent_version import FluentVersion @@ -76,14 +79,14 @@ def f(): timeout_loop( lambda: (inside_container and not get_container(cortex_host)) - or (not inside_container and not psutil.pid_exists(fluent_host_pid)), + or (not inside_container and not _pid_exists(fluent_host_pid)), 60, ) if inside_container: assert not get_container(cortex_host) else: - assert not psutil.pid_exists(fluent_host_pid) + assert not _pid_exists(fluent_host_pid) def test_server_does_not_exit_when_session_goes_out_of_scope() -> None: @@ -104,7 +107,7 @@ def f(): else: from pathlib import Path - assert psutil.pid_exists(fluent_host_pid) + assert _pid_exists(fluent_host_pid) if os.name == "nt": cleanup_file_ext = "bat" cmd_list = [] @@ -243,7 +246,7 @@ def test_fluent_exit(monkeypatch: pytest.MonkeyPatch): solver.exit() assert timeout_loop( lambda: (inside_container and not get_container(cortex)) - or (not inside_container and not psutil.pid_exists(cortex)), + or (not inside_container and not _pid_exists(cortex)), timeout=60, idle_period=1, )