33
44import argparse
55import sys
6+ import threading
67from datetime import datetime , timezone
78from pathlib import Path
89
@@ -139,14 +140,24 @@ def _parse_model_arg(val: str) -> tuple[str | None, str]:
139140
140141
141142def _make_progress_callbacks (console : Console ):
142- """Build (on_item_start, on_run_done, on_item_done) callbacks that stream progress."""
143+ """Build (on_item_start, on_run_done, on_item_done) callbacks that stream progress.
144+
145+ A threading lock guards all console.print() calls so that concurrent
146+ ``--concurrency 2+`` workers do not deadlock when stdout is piped
147+ (e.g. running in a background process).
148+ """
149+ _print_lock = threading .Lock ()
143150
144151 def on_item_start (index : int , total : int , item : DatasetItem ) -> None :
145- console .print (f"[dim]\\ [{ index } /{ total } ][/dim] [cyan]{ item .id } [/cyan] { _truncate (item .question )} " )
152+ with _print_lock :
153+ console .print (f"[dim]\\ [{ index } /{ total } ][/dim] [cyan]{ item .id } [/cyan] { _truncate (item .question )} " )
146154
147155 def on_run_done (index : int , total : int , run_index : int , runs : int , passed : bool , latency : float ) -> None :
148156 tag = "[green]pass[/green]" if passed else "[red]fail[/red]"
149- console .print (f"[dim]\\ [{ index } /{ total } ][/dim] run { run_index } /{ runs } { tag } [dim]{ latency :.2f} s[/dim]" )
157+ with _print_lock :
158+ console .print (
159+ f"[dim]\\ [{ index } /{ total } ][/dim] run { run_index } /{ runs } { tag } [dim]{ latency :.2f} s[/dim]"
160+ )
150161
151162 def on_item_done (index : int , total : int , report : ItemReport ) -> None :
152163 if report .skipped :
@@ -165,7 +176,8 @@ def on_item_done(index: int, total: int, report: ItemReport) -> None:
165176 f" [dim]({ report .latency_s :.2f} s total, { report .avg_latency_s :.2f} s avg, "
166177 f"quality={ quality_str } , { report .runs } run(s))[/dim]"
167178 )
168- console .print (f"[dim]\\ [{ index } /{ total } ][/dim] -> { tag } [cyan]{ report .id } [/cyan]{ suffix } " )
179+ with _print_lock :
180+ console .print (f"[dim]\\ [{ index } /{ total } ][/dim] -> { tag } [cyan]{ report .id } [/cyan]{ suffix } " )
169181
170182 return on_item_start , on_run_done , on_item_done
171183
0 commit comments