1616
1717from __future__ import absolute_import
1818
19+ import multiprocessing
1920import os
2021import pathlib
2122import re
2223import shutil
24+ import time
25+ import traceback
2326from typing import Dict , List
2427import 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 )
799859def benchmark (session : nox .Session ):
800860 session .install ("-e" , ".[all]" )
0 commit comments