Skip to content

Commit bbe1c69

Browse files
committed
fix: updated server and process closing logic after sphinx-gallery execution
1 parent 85dda6e commit bbe1c69

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

doc/source/conf.py

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
ignored_pattern += f"|{example_name}"
6565
ignored_pattern += "|11-server_types.py"
6666
ignored_pattern += "|06-distributed_stress_averaging.py"
67-
ignored_pattern += "|00-wrapping_numpy_capabilities.py"
68-
ignored_pattern += "|01-package_python_operators.py"
67+
# ignored_pattern += "|00-wrapping_numpy_capabilities.py"
68+
# ignored_pattern += "|01-package_python_operators.py"
6969
ignored_pattern += "|02-python_operators_with_dependencies.py"
7070
ignored_pattern += r")"
7171

@@ -330,7 +330,7 @@ def reset_servers(gallery_conf, fname, when):
330330
epub_exclude_files = ["search.html"]
331331

332332

333-
def check_global_servers_and_close(app: sphinx.application.Sphinx, exception: Exception) -> None:
333+
def close_live_servers_and_processes(app: sphinx.application.Sphinx) -> None:
334334
from ansys.dpf.core import server, _server_instances
335335
import copy
336336
import psutil
@@ -339,24 +339,27 @@ def check_global_servers_and_close(app: sphinx.application.Sphinx, exception: Ex
339339
gc.collect
340340

341341
# Server instances are different from processes.
342-
# Ideally just closing the servers => no running processes
342+
# Ideally no running servers => no running processes
343343
# Server instances should be closed first
344-
if _server_instances:
345-
print(f"{len(_server_instances)} dpf server instances found running")
346-
print("Closing server instances")
344+
running_servers = sum([1 if instance().live else 0 for instance in _server_instances])
345+
if running_servers:
346+
print(f"{running_servers} live dpf servers found")
347+
print("Closing servers")
347348

348349
copy_instances = copy.deepcopy(_server_instances)
349350
for instance in copy_instances:
350351
try:
351-
if hasattr(instance(), "shutdown"):
352-
instance().shutdown()
352+
instance_object = instance()
353+
if hasattr(instance_object, "shutdown"):
354+
instance_object.shutdown()
353355
except Exception as e:
354356
print(e.args)
355357
pass
356358
server.shutdown_global_server()
357-
print(f"{len(_server_instances)} dpf server instances found running after closing")
359+
running_servers = sum([1 if instance().live else 0 for instance in _server_instances])
360+
print(f"Found {running_servers} live dpf servers after closing")
358361
else:
359-
print("no server instances found running")
362+
print("No live dpf servers found")
360363

361364
# Subsequently check running processes and close
362365
proc_name = "Ans.Dpf.Grpc"
@@ -376,32 +379,6 @@ def check_global_servers_and_close(app: sphinx.application.Sphinx, exception: Ex
376379
else:
377380
print(f"No processes were found running")
378381

379-
# def reset_server_after_sphinx_gallery(app: sphinx.application.Sphinx, exception: Exception) -> None:
380-
# """
381-
# Stop running dpf servers before regardless of documentation build sucess.
382-
383-
# Parameters
384-
# ----------
385-
# app : sphinx.application.Sphinx
386-
# Sphinx application instance containing the all the doc build configuration.
387-
388-
# """
389-
# from ansys.dpf.core import server
390-
391-
# logger = logging.getLogger(__name__)
392-
393-
# try:
394-
# server.check_global_servers_and_close()
395-
# except exception:
396-
# if "Extension error" in str(exception):
397-
# server.check_global_servers_and_close()
398-
# logger.warning("An error occurred during sphinx gallery execution")
399-
# raise exception
400-
# else:
401-
# logger.warning("An error outside sphinx gallery execution occurred")
402-
# raise exception
403-
404-
405382
def setup(app: sphinx.application.Sphinx) -> None:
406383
"""
407384
Run hook function(s) during the documentation build.
@@ -412,5 +389,4 @@ def setup(app: sphinx.application.Sphinx) -> None:
412389
Sphinx application instance containing the all the doc build configuration.
413390
"""
414391

415-
app.connect("builder-inited", check_global_servers_and_close)
416-
app.connect("build-finished", check_global_servers_and_close)
392+
app.connect("builder-inited", close_live_servers_and_processes)

0 commit comments

Comments
 (0)