Skip to content

Commit

Permalink
fix: Remove psutil dependency (#3439)
Browse files Browse the repository at this point in the history
* fix: remove psutil

* fix: remove psutil

* fix: remove psutil

* fix: remove psutil

* fix: remove psutil

* fix: test fix

* fix: watchdog
  • Loading branch information
hpohekar authored Nov 5, 2024
1 parent 0b337d1 commit e545db6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
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_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.
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
9 changes: 6 additions & 3 deletions src/ansys/fluent/core/launcher/watchdog_exec
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)

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

0 comments on commit e545db6

Please sign in to comment.