haiyizxx commited on
Commit
8580f79
·
1 Parent(s): 7c430fa

fix: stream subprocess output in real-time

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -38,10 +38,10 @@ def _clone_repo(workdir: Path) -> Path:
38
  return repo_dir
39
 
40
 
41
- def _run_finish_review(repo_dir: Path, task_id: int) -> str:
42
- result = subprocess.run(
43
  [
44
- sys.executable, str(repo_dir / "scripts" / "finish_review.py"),
45
  "--task-id", str(task_id),
46
  "--dataset", DATASET,
47
  "--experiment", f"cvat_review_{task_id}",
@@ -49,11 +49,16 @@ def _run_finish_review(repo_dir: Path, task_id: int) -> str:
49
  "--cvat-url", CVAT_URL,
50
  "--cvat-token", CVAT_TOKEN,
51
  ],
52
- capture_output=True,
 
53
  text=True,
54
  cwd=str(repo_dir),
55
  )
56
- return result.stdout + result.stderr
 
 
 
 
57
 
58
 
59
  @app.post("/webhook")
@@ -82,14 +87,13 @@ async def cvat_webhook(request: Request):
82
  try:
83
  with tempfile.TemporaryDirectory() as workdir:
84
  repo_dir = _clone_repo(Path(workdir))
85
- print(f"Running finish_review for task {tid}...")
86
- output = _run_finish_review(repo_dir, tid)
87
- print(output)
88
- print(f"finish_review completed for task {tid}")
89
  except subprocess.TimeoutExpired:
90
- print(f"finish_review timed out for task {tid}")
91
  except Exception as exc:
92
- print(f"finish_review failed for task {tid}: {exc}")
93
 
94
  threading.Thread(target=_run_in_background, args=(task_id,), daemon=True).start()
95
 
 
38
  return repo_dir
39
 
40
 
41
+ def _run_finish_review(repo_dir: Path, task_id: int) -> None:
42
+ proc = subprocess.Popen(
43
  [
44
+ sys.executable, "-u", str(repo_dir / "scripts" / "finish_review.py"),
45
  "--task-id", str(task_id),
46
  "--dataset", DATASET,
47
  "--experiment", f"cvat_review_{task_id}",
 
49
  "--cvat-url", CVAT_URL,
50
  "--cvat-token", CVAT_TOKEN,
51
  ],
52
+ stdout=subprocess.PIPE,
53
+ stderr=subprocess.STDOUT,
54
  text=True,
55
  cwd=str(repo_dir),
56
  )
57
+ for line in proc.stdout:
58
+ print(line, end="", flush=True)
59
+ proc.wait()
60
+ if proc.returncode != 0:
61
+ raise RuntimeError(f"finish_review exited with code {proc.returncode}")
62
 
63
 
64
  @app.post("/webhook")
 
87
  try:
88
  with tempfile.TemporaryDirectory() as workdir:
89
  repo_dir = _clone_repo(Path(workdir))
90
+ print(f"Running finish_review for task {tid}...", flush=True)
91
+ _run_finish_review(repo_dir, tid)
92
+ print(f"finish_review completed for task {tid}", flush=True)
 
93
  except subprocess.TimeoutExpired:
94
+ print(f"finish_review timed out for task {tid}", flush=True)
95
  except Exception as exc:
96
+ print(f"finish_review failed for task {tid}: {exc}", flush=True)
97
 
98
  threading.Thread(target=_run_in_background, args=(task_id,), daemon=True).start()
99