Skip to content

Commit da3524b

Browse files
chore: update notebook session to run faster. (#970)
* chore: update notebook session to run faster. * update lint * separate to two runs. * update code * update code * update format * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix nbmake internal error. --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 8fbfb9a commit da3524b

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

noxfile.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
from __future__ import absolute_import
1818

19+
import multiprocessing
1920
import os
2021
import pathlib
2122
import re
2223
import shutil
24+
import time
25+
import traceback
2326
from typing import Dict, List
2427
import warnings
2528

@@ -754,6 +757,12 @@ def notebook(session: nox.Session):
754757
for nb in notebooks + list(notebooks_reg):
755758
assert os.path.exists(nb), nb
756759

760+
# Determine whether to enable multi-process mode based on the environment
761+
# variable. If BENCHMARK_AND_PUBLISH is "true", it indicates we're running
762+
# a benchmark, so we disable multi-process mode. If BENCHMARK_AND_PUBLISH
763+
# is "false", we enable multi-process mode for faster execution.
764+
multi_process_mode = os.getenv("BENCHMARK_AND_PUBLISH", "false") == "false"
765+
757766
try:
758767
# Populate notebook parameters and make a backup so that the notebooks
759768
# are runnable.
@@ -762,23 +771,65 @@ def notebook(session: nox.Session):
762771
CURRENT_DIRECTORY / "scripts" / "notebooks_fill_params.py",
763772
*notebooks,
764773
)
774+
775+
# Shared flag using multiprocessing.Manager() to indicate if
776+
# any process encounters an error. This flag may be updated
777+
# across different processes.
778+
error_flag = multiprocessing.Manager().Value("i", False)
779+
processes = []
765780
for notebook in notebooks:
766-
session.run(
781+
args = (
767782
"python",
768783
"scripts/run_and_publish_benchmark.py",
769784
"--notebook",
770785
f"--benchmark-path={notebook}",
771786
)
772-
787+
if multi_process_mode:
788+
process = multiprocessing.Process(
789+
target=_run_process,
790+
args=(session, args, error_flag),
791+
)
792+
process.start()
793+
processes.append(process)
794+
# Adding a small delay between starting each
795+
# process to avoid potential race conditions。
796+
time.sleep(1)
797+
else:
798+
session.run(*args)
799+
800+
for process in processes:
801+
process.join()
802+
803+
processes = []
773804
for notebook, regions in notebooks_reg.items():
774805
for region in regions:
775-
session.run(
806+
args = (
776807
"python",
777808
"scripts/run_and_publish_benchmark.py",
778809
"--notebook",
779810
f"--benchmark-path={notebook}",
780811
f"--region={region}",
781812
)
813+
if multi_process_mode:
814+
process = multiprocessing.Process(
815+
target=_run_process,
816+
args=(session, args, error_flag),
817+
)
818+
process.start()
819+
processes.append(process)
820+
# Adding a small delay between starting each
821+
# process to avoid potential race conditions。
822+
time.sleep(1)
823+
else:
824+
session.run(*args)
825+
826+
for process in processes:
827+
process.join()
828+
829+
# Check the shared error flag and raise an exception if any process
830+
# reported an error
831+
if error_flag.value:
832+
raise Exception("Errors occurred in one or more subprocesses.")
782833
finally:
783834
# Prevent our notebook changes from getting checked in to git
784835
# accidentally.
@@ -795,6 +846,15 @@ def notebook(session: nox.Session):
795846
)
796847

797848

849+
def _run_process(session: nox.Session, args, error_flag):
850+
try:
851+
session.run(*args)
852+
except Exception:
853+
traceback_str = traceback.format_exc()
854+
print(traceback_str)
855+
error_flag.value = True
856+
857+
798858
@nox.session(python=DEFAULT_PYTHON_VERSION)
799859
def benchmark(session: nox.Session):
800860
session.install("-e", ".[all]")

0 commit comments

Comments
 (0)