16
16
17
17
from __future__ import absolute_import
18
18
19
+ import multiprocessing
19
20
import os
20
21
import pathlib
21
22
import re
22
23
import shutil
24
+ import time
25
+ import traceback
23
26
from typing import Dict , List
24
27
import warnings
25
28
@@ -754,6 +757,12 @@ def notebook(session: nox.Session):
754
757
for nb in notebooks + list (notebooks_reg ):
755
758
assert os .path .exists (nb ), nb
756
759
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
+
757
766
try :
758
767
# Populate notebook parameters and make a backup so that the notebooks
759
768
# are runnable.
@@ -762,23 +771,65 @@ def notebook(session: nox.Session):
762
771
CURRENT_DIRECTORY / "scripts" / "notebooks_fill_params.py" ,
763
772
* notebooks ,
764
773
)
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 = []
765
780
for notebook in notebooks :
766
- session . run (
781
+ args = (
767
782
"python" ,
768
783
"scripts/run_and_publish_benchmark.py" ,
769
784
"--notebook" ,
770
785
f"--benchmark-path={ notebook } " ,
771
786
)
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 = []
773
804
for notebook , regions in notebooks_reg .items ():
774
805
for region in regions :
775
- session . run (
806
+ args = (
776
807
"python" ,
777
808
"scripts/run_and_publish_benchmark.py" ,
778
809
"--notebook" ,
779
810
f"--benchmark-path={ notebook } " ,
780
811
f"--region={ region } " ,
781
812
)
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." )
782
833
finally :
783
834
# Prevent our notebook changes from getting checked in to git
784
835
# accidentally.
@@ -795,6 +846,15 @@ def notebook(session: nox.Session):
795
846
)
796
847
797
848
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
+
798
858
@nox .session (python = DEFAULT_PYTHON_VERSION )
799
859
def benchmark (session : nox .Session ):
800
860
session .install ("-e" , ".[all]" )
0 commit comments