Skip to content

Commit

Permalink
fix: modified function checking server closure after sphinx gallery e…
Browse files Browse the repository at this point in the history
…xecution
  • Loading branch information
moe-ad committed Dec 11, 2024
1 parent bbe1c69 commit 32954d1
Showing 1 changed file with 6 additions and 34 deletions.
40 changes: 6 additions & 34 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,37 +331,15 @@ def reset_servers(gallery_conf, fname, when):


def close_live_servers_and_processes(app: sphinx.application.Sphinx) -> None:
from ansys.dpf.core import server, _server_instances
import copy
# Adapted from reset_servers() function, so this can be called after
# sphinx gallery finishes execution
import psutil
from ansys.dpf.core import server
import gc

gc.collect

# Server instances are different from processes.
# Ideally no running servers => no running processes
# Server instances should be closed first
running_servers = sum([1 if instance().live else 0 for instance in _server_instances])
if running_servers:
print(f"{running_servers} live dpf servers found")
print("Closing servers")

copy_instances = copy.deepcopy(_server_instances)
for instance in copy_instances:
try:
instance_object = instance()
if hasattr(instance_object, "shutdown"):
instance_object.shutdown()
except Exception as e:
print(e.args)
pass
server.shutdown_global_server()
running_servers = sum([1 if instance().live else 0 for instance in _server_instances])
print(f"Found {running_servers} live dpf servers after closing")
else:
print("No live dpf servers found")

# Subsequently check running processes and close
gc.collect()
server.shutdown_all_session_servers()

proc_name = "Ans.Dpf.Grpc"
nb_procs = 0
for proc in psutil.process_iter():
Expand All @@ -373,12 +351,6 @@ def close_live_servers_and_processes(app: sphinx.application.Sphinx) -> None:
except psutil.NoSuchProcess:
pass

# Check if processes were actually killed
if nb_procs:
print(f"Killed {nb_procs} {proc_name} processes.")
else:
print(f"No processes were found running")

def setup(app: sphinx.application.Sphinx) -> None:
"""
Run hook function(s) during the documentation build.
Expand Down

0 comments on commit 32954d1

Please sign in to comment.