From fb8125f0cfeeca0afe7f6423e3cb7034860cb46b Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 31 Oct 2024 17:51:22 +0530 Subject: [PATCH 1/7] fix: remove psutil --- src/ansys/fluent/core/fluent_connection.py | 14 +++++++++++--- tests/test_fluent_session.py | 15 +++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/ansys/fluent/core/fluent_connection.py b/src/ansys/fluent/core/fluent_connection.py index 5934ee28dbc..36b026c7928 100644 --- a/src/ansys/fluent/core/fluent_connection.py +++ b/src/ansys/fluent/core/fluent_connection.py @@ -16,7 +16,6 @@ import weakref import grpc -import psutil import ansys.fluent.core as pyfluent from ansys.fluent.core.services import service_creator @@ -292,6 +291,15 @@ def exit_server(self): self.scheme_eval.exec(("(exit-server)",)) +def _pid_exists(pid): + try: + os.kill(pid, 0) + except OSError: + return False + else: + return True + + class FluentConnection: """Encapsulates a Fluent connection. @@ -601,8 +609,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/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, ) From d4fec1bec4ff724b7ba6bf8c68f78ee42737d279 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 31 Oct 2024 18:51:34 +0530 Subject: [PATCH 2/7] fix: remove psutil --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index db656e8e7a2..91203cd5bc0 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 } From 30d7ac72346d3c6a05a7ca20867e9f3e6e762139 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 31 Oct 2024 21:07:25 +0530 Subject: [PATCH 3/7] fix: remove psutil --- src/ansys/fluent/core/fluent_connection.py | 37 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/ansys/fluent/core/fluent_connection.py b/src/ansys/fluent/core/fluent_connection.py index 36b026c7928..93f973ee08f 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 @@ -292,12 +294,35 @@ def exit_server(self): def _pid_exists(pid): - try: - os.kill(pid, 0) - except OSError: - return False - else: - return True + if platform.system() == "Linux": + try: + os.kill(pid, 0) + except OSError: + return False + else: + return True + elif platform.system() == "Windows": + # try: + # command = f'tasklist /FI "PID eq {pid}"' + # result = subprocess.run(command, shell=True, text=True, capture_output=True) + # if result.returncode == 0: + # output = result.stdout.strip() + # lines = output.splitlines() + # if len(lines) == 3 and str(pid) in lines[2]: + # return True + # else: + # return False + # except Exception as e: + # return False + PROCESS_QUERY_INFORMATION = 0x1000 + process_handle = ctypes.windll.kernel32.OpenProcess( + PROCESS_QUERY_INFORMATION, 0, pid + ) + if process_handle == 0: + return False + else: + ctypes.windll.kernel32.CloseHandle(process_handle) + return True class FluentConnection: From bef1c7460c9a762188a6292b5d94fbf5570935f3 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 31 Oct 2024 21:09:29 +0530 Subject: [PATCH 4/7] fix: remove psutil --- src/ansys/fluent/core/fluent_connection.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/ansys/fluent/core/fluent_connection.py b/src/ansys/fluent/core/fluent_connection.py index 93f973ee08f..c7ef189701d 100644 --- a/src/ansys/fluent/core/fluent_connection.py +++ b/src/ansys/fluent/core/fluent_connection.py @@ -302,21 +302,9 @@ def _pid_exists(pid): else: return True elif platform.system() == "Windows": - # try: - # command = f'tasklist /FI "PID eq {pid}"' - # result = subprocess.run(command, shell=True, text=True, capture_output=True) - # if result.returncode == 0: - # output = result.stdout.strip() - # lines = output.splitlines() - # if len(lines) == 3 and str(pid) in lines[2]: - # return True - # else: - # return False - # except Exception as e: - # return False - PROCESS_QUERY_INFORMATION = 0x1000 + process_query_information = 0x1000 process_handle = ctypes.windll.kernel32.OpenProcess( - PROCESS_QUERY_INFORMATION, 0, pid + process_query_information, 0, pid ) if process_handle == 0: return False From 26c1d295f4186bb7f34ccc64be9c8e5e03aaaf09 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Thu, 31 Oct 2024 21:25:28 +0530 Subject: [PATCH 5/7] fix: remove psutil --- tests/test_fluent_session.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/tests/test_fluent_session.py b/tests/test_fluent_session.py index 1a2e3e596be..143f6c981db 100644 --- a/tests/test_fluent_session.py +++ b/tests/test_fluent_session.py @@ -66,15 +66,32 @@ def print_transcript(transcript): assert not print_transcript.called -def test_server_exits_when_session_goes_out_of_scope() -> None: - def f(): - session = pyfluent.launch_fluent() - session.settings - _fluent_host_pid = session.connection_properties.fluent_host_pid - _cortex_host = session.connection_properties.cortex_host - _inside_container = session.connection_properties.inside_container - return _fluent_host_pid, _cortex_host, _inside_container +def f(): + session = pyfluent.launch_fluent() + session.settings + _fluent_host_pid = session.connection_properties.fluent_host_pid + _cortex_host = session.connection_properties.cortex_host + _inside_container = session.connection_properties.inside_container + return _fluent_host_pid, _cortex_host, _inside_container + + +def test_server_exits_when_session_goes_out_of_scope_on_linux() -> None: + fluent_host_pid, cortex_host, inside_container = f() + + timeout_loop( + lambda: (inside_container and not get_container(cortex_host)) + or (not inside_container and not _pid_exists(fluent_host_pid)), + 60, + ) + + if inside_container: + assert not get_container(cortex_host) + else: + assert not _pid_exists(fluent_host_pid) + +@pytest.mark.standalone +def test_server_exits_when_session_goes_out_of_scope_on_windows() -> None: fluent_host_pid, cortex_host, inside_container = f() timeout_loop( From 304e36ed92e883801856d04ed4baaa434face86a Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 1 Nov 2024 08:48:33 +0530 Subject: [PATCH 6/7] fix: test fix --- src/ansys/fluent/core/fluent_connection.py | 4 +-- tests/test_fluent_session.py | 33 ++++++---------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/src/ansys/fluent/core/fluent_connection.py b/src/ansys/fluent/core/fluent_connection.py index c7ef189701d..a45437848ca 100644 --- a/src/ansys/fluent/core/fluent_connection.py +++ b/src/ansys/fluent/core/fluent_connection.py @@ -302,9 +302,9 @@ def _pid_exists(pid): else: return True elif platform.system() == "Windows": - process_query_information = 0x1000 + process_query_limited_information = 0x1000 process_handle = ctypes.windll.kernel32.OpenProcess( - process_query_information, 0, pid + process_query_limited_information, 0, pid ) if process_handle == 0: return False diff --git a/tests/test_fluent_session.py b/tests/test_fluent_session.py index 143f6c981db..1a2e3e596be 100644 --- a/tests/test_fluent_session.py +++ b/tests/test_fluent_session.py @@ -66,32 +66,15 @@ def print_transcript(transcript): assert not print_transcript.called -def f(): - session = pyfluent.launch_fluent() - session.settings - _fluent_host_pid = session.connection_properties.fluent_host_pid - _cortex_host = session.connection_properties.cortex_host - _inside_container = session.connection_properties.inside_container - return _fluent_host_pid, _cortex_host, _inside_container - - -def test_server_exits_when_session_goes_out_of_scope_on_linux() -> None: - fluent_host_pid, cortex_host, inside_container = f() - - timeout_loop( - lambda: (inside_container and not get_container(cortex_host)) - or (not inside_container and not _pid_exists(fluent_host_pid)), - 60, - ) - - if inside_container: - assert not get_container(cortex_host) - else: - assert not _pid_exists(fluent_host_pid) - +def test_server_exits_when_session_goes_out_of_scope() -> None: + def f(): + session = pyfluent.launch_fluent() + session.settings + _fluent_host_pid = session.connection_properties.fluent_host_pid + _cortex_host = session.connection_properties.cortex_host + _inside_container = session.connection_properties.inside_container + return _fluent_host_pid, _cortex_host, _inside_container -@pytest.mark.standalone -def test_server_exits_when_session_goes_out_of_scope_on_windows() -> None: fluent_host_pid, cortex_host, inside_container = f() timeout_loop( From d10f61ca5eb018a3d3b419543c9d1be83bdb2018 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 1 Nov 2024 08:52:36 +0530 Subject: [PATCH 7/7] fix: watchdog --- src/ansys/fluent/core/launcher/watchdog_exec | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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)