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
14 changes: 11 additions & 3 deletions src/ansys/fluent/core/fluent_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 +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.

Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 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 @@ -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:
Expand All @@ -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 = []
Expand Down Expand Up @@ -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,
)
Expand Down