1818
1919import pydantic .v1 as pd
2020from pydantic .v1 import PrivateAttr
21- from rich .progress import BarColumn , Progress , TaskProgressColumn , TextColumn , TimeElapsedColumn
21+ from rich .progress import (
22+ BarColumn ,
23+ Progress ,
24+ TaskID ,
25+ TaskProgressColumn ,
26+ TextColumn ,
27+ TimeElapsedColumn ,
28+ )
2229
2330from tidy3d .components .base import Tidy3dBaseModel , cached_property
2431from tidy3d .components .mode .mode_solver import ModeSolver
@@ -331,7 +338,7 @@ def load_if_cached(self) -> bool:
331338 simulation = self .simulation ,
332339 path = stash_path ,
333340 reduce_simulation = self .reduce_simulation ,
334- verbose = getattr ( self , " verbose" , True ) ,
341+ verbose = self . verbose ,
335342 )
336343
337344 if restored is None :
@@ -945,27 +952,42 @@ def upload(self) -> None:
945952 """Upload a series of tasks associated with this ``Batch`` using multi-threading."""
946953 self ._check_folder (self .folder_name )
947954 with ThreadPoolExecutor (max_workers = self .num_workers ) as executor :
948- futures = [executor .submit (job .upload ) for _ , job in self .jobs .items ()]
955+ jobs_from_cache = [job for job in self .jobs .values () if job .load_if_cached ]
956+ jobs_to_upload = [job for job in self .jobs .values () if not job .load_if_cached ]
957+ futures = [executor .submit (job .upload ) for job in jobs_to_upload ]
949958
950959 # progressbar (number of tasks uploaded)
951960 if self .verbose :
952961 console = get_logging_console ()
962+ n_cached = len (jobs_from_cache )
963+ if n_cached > 0 :
964+ console .log (
965+ f"Got { n_cached } simulation{ 's' if n_cached > 1 else '' } from cache."
966+ )
967+
968+ if len (futures ) == 0 : # got all jobs from cache
969+ return
970+
971+ if self .verbose :
953972 progress_columns = (
954973 TextColumn ("[progress.description]{task.description}" ),
955974 BarColumn (),
956975 TaskProgressColumn (),
957976 TimeElapsedColumn (),
958977 )
959978 with Progress (* progress_columns , console = console , transient = False ) as progress :
960- pbar_message = f"Uploading data for { self . num_jobs } tasks "
961- pbar = progress .add_task (pbar_message , total = self . num_jobs )
979+ pbar_message = f"Uploading data for { len ( jobs_to_upload ) } task { 's' if len ( jobs_to_upload ) > 1 else '' } "
980+ pbar = progress .add_task (pbar_message , total = len ( jobs_to_upload ) )
962981 completed = 0
963982 for _ in concurrent .futures .as_completed (futures ):
964983 completed += 1
965984 progress .update (pbar , completed = completed )
966985
967986 progress .refresh ()
968987 time .sleep (BATCH_PROGRESS_REFRESH_TIME )
988+ else :
989+ for _ in concurrent .futures .as_completed (futures ):
990+ pass
969991
970992 def get_info (self ) -> dict [TaskName , TaskInfo ]:
971993 """Get information about each task in the :class:`Batch`.
@@ -1138,6 +1160,8 @@ def pbar_description(
11381160 postprocess_started_tasks : set [str ] = set ()
11391161
11401162 try :
1163+ console = None
1164+ progress_columns = []
11411165 if self .verbose :
11421166 console = get_logging_console ()
11431167 self .estimate_cost ()
@@ -1152,34 +1176,38 @@ def pbar_description(
11521176 TimeElapsedColumn (),
11531177 )
11541178
1155- with Progress (* progress_columns , console = console , transient = False ) as progress :
1156- pbar_tasks : dict [str , int ] = {}
1157- for task_name , job in self .jobs .items ():
1158- schedule_download (job )
1179+ with Progress (
1180+ * progress_columns , console = console , transient = False , disable = not self .verbose
1181+ ) as progress :
1182+ pbar_tasks : dict [str , TaskID ] = {}
1183+ for task_name , job in self .jobs .items ():
1184+ schedule_download (job )
1185+ if self .verbose :
11591186 status = job .status
11601187 completed = STATE_PROGRESS_PERCENTAGE .get (status , 0 )
11611188 desc = pbar_description (task_name , status , max_name_length , 0 )
11621189 pbar_tasks [task_name ] = progress .add_task (
11631190 desc , total = COMPLETED_PERCENT , completed = completed
11641191 )
11651192
1166- while any (check_continue_condition (job ) for job in self .jobs .values ()):
1167- for task_name , job in self .jobs .items ():
1168- status = job .status
1169-
1170- # auto-start postprocess for modeler jobs when run finishes
1171- if (
1172- web ._is_modeler_batch (job .task_id )
1173- and status == "run_success"
1174- and job .task_id not in postprocess_started_tasks
1175- ):
1176- job .postprocess_start (
1177- worker_group = postprocess_worker_group , verbose = True
1178- )
1179- postprocess_started_tasks .add (job .task_id )
1193+ while any (check_continue_condition (job ) for job in self .jobs .values ()):
1194+ for task_name , job in self .jobs .items ():
1195+ status = job .status
1196+
1197+ # auto-start postprocess for modeler jobs when run finishes
1198+ if (
1199+ web ._is_modeler_batch (job .task_id )
1200+ and status == "run_success"
1201+ and job .task_id not in postprocess_started_tasks
1202+ ):
1203+ job .postprocess_start (
1204+ worker_group = postprocess_worker_group , verbose = True
1205+ )
1206+ postprocess_started_tasks .add (job .task_id )
11801207
1181- schedule_download (job )
1208+ schedule_download (job )
11821209
1210+ if self .verbose :
11831211 # choose display status & percent
11841212 if status != "run_success" :
11851213 display_status = status
@@ -1196,13 +1224,17 @@ def pbar_description(
11961224 pbar = pbar_tasks [task_name ]
11971225 desc = pbar_description (task_name , display_status , max_name_length , 0 )
11981226 progress .update (pbar , description = desc , completed = pct )
1199-
1227+ if self . verbose :
12001228 progress .refresh ()
12011229 time .sleep (BATCH_PROGRESS_REFRESH_TIME )
1230+ else :
1231+ time .sleep (web .REFRESH_TIME )
12021232
1203- # final render to terminal state for all bars
1204- for task_name , job in self .jobs .items ():
1205- schedule_download (job )
1233+ # final render to terminal state for all bars
1234+ for task_name , job in self .jobs .items ():
1235+ schedule_download (job )
1236+
1237+ if self .verbose :
12061238 status = job .status
12071239 if status != "run_success" :
12081240 display_status = status
@@ -1222,25 +1254,9 @@ def pbar_description(
12221254 desc = pbar_description (task_name , display_status , max_name_length , 0 )
12231255 progress .update (pbar , description = desc , completed = pct )
12241256
1257+ if self .verbose :
12251258 progress .refresh ()
12261259 console .log ("Batch complete." )
1227- else :
1228- # quiet mode
1229- while any (check_continue_condition (job ) for job in self .jobs .values ()):
1230- for job in self .jobs .values ():
1231- if (
1232- web ._is_modeler_batch (job .task_id )
1233- and job .status == "run_success"
1234- and job .task_id not in postprocess_started_tasks
1235- ):
1236- job .postprocess_start (
1237- worker_group = postprocess_worker_group , verbose = False
1238- )
1239- postprocess_started_tasks .add (job .task_id )
1240-
1241- schedule_download (job )
1242-
1243- time .sleep (web .REFRESH_TIME )
12441260 finally :
12451261 if download_executor is not None :
12461262 try :
0 commit comments