Skip to content
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

fix: Remove psutil dependency #3439

Merged
merged 10 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
27 changes: 24 additions & 3 deletions src/ansys/fluent/core/fluent_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +18,6 @@
import weakref

import grpc
import psutil

import ansys.fluent.core as pyfluent
from ansys.fluent.core.services import service_creator
Expand Down Expand Up @@ -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_information = 0x1000
hpohekar marked this conversation as resolved.
Show resolved Hide resolved
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:
"""Encapsulates a Fluent connection.

Expand Down Expand Up @@ -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,
Expand Down
48 changes: 34 additions & 14 deletions tests/test_fluent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,27 +66,44 @@ 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:
hpohekar marked this conversation as resolved.
Show resolved Hide resolved
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 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:
Expand All @@ -104,7 +124,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 = []
Expand Down Expand Up @@ -243,7 +263,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,
)
Expand Down
Loading