From 40c1a38995b1bbe02abdab643e0961bbdbcf8eff Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Wed, 8 Apr 2026 20:07:05 +0800 Subject: [PATCH] fix: Windows-compatible Manim subprocess with streaming output Replace asyncio.create_subprocess_exec with subprocess.Popen + reader threads + asyncio.Queue. This fixes Windows SelectorEventLoop incompatibility while preserving real-time line-by-line progress output (unlike subprocess.run which buffers until completion). Made-with: Cursor --- deeptutor/agents/math_animator/renderer.py | 70 ++++++++++++---------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/deeptutor/agents/math_animator/renderer.py b/deeptutor/agents/math_animator/renderer.py index 32c06dd22..5ee89a44c 100644 --- a/deeptutor/agents/math_animator/renderer.py +++ b/deeptutor/agents/math_animator/renderer.py @@ -4,7 +4,9 @@ from __future__ import annotations import asyncio import re +import subprocess import sys +import threading from pathlib import Path from typing import Awaitable, Callable @@ -145,43 +147,51 @@ class ManimRenderService: else: command.extend(["--format", "mp4"]) - process = await asyncio.create_subprocess_exec( - *command, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE, - ) await self._emit_progress( f"Started Manim process for `{scene_name}` with command: {' '.join(command)}", raw=True, ) + + # Use subprocess.Popen instead of asyncio.create_subprocess_exec + # for Windows compatibility (SelectorEventLoop doesn't support + # asyncio subprocesses). Reader threads + asyncio.Queue preserve + # real-time streaming output. + process = subprocess.Popen( + command, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + + _SENTINEL = None + queue: asyncio.Queue[tuple[str, str] | None] = asyncio.Queue() + loop = asyncio.get_running_loop() + + def _reader(stream, prefix: str) -> None: + assert stream is not None + for raw_line in stream: + line = raw_line.decode(errors="ignore").strip() + if line: + loop.call_soon_threadsafe(queue.put_nowait, (prefix, line)) + loop.call_soon_threadsafe(queue.put_nowait, _SENTINEL) + + threading.Thread(target=_reader, args=(process.stdout, "stdout"), daemon=True).start() + threading.Thread(target=_reader, args=(process.stderr, "stderr"), daemon=True).start() + stdout_lines: list[str] = [] stderr_lines: list[str] = [] + streams_open = 2 + while streams_open > 0: + item = await queue.get() + if item is _SENTINEL: + streams_open -= 1 + continue + prefix, line = item + (stdout_lines if prefix == "stdout" else stderr_lines).append(line) + await self._emit_progress(f"[{prefix}] {line}", raw=True) - async def _drain_stream( - stream: asyncio.StreamReader | None, - sink: list[str], - *, - prefix: str, - ) -> None: - if stream is None: - return - while True: - raw_line = await stream.readline() - if not raw_line: - break - line = raw_line.decode(errors="ignore").strip() - if not line: - continue - sink.append(line) - await self._emit_progress(f"[{prefix}] {line}", raw=True) - - await asyncio.gather( - _drain_stream(process.stdout, stdout_lines, prefix="stdout"), - _drain_stream(process.stderr, stderr_lines, prefix="stderr"), - ) - await process.wait() - await self._emit_progress(f"Manim process finished with exit code {process.returncode}.", raw=True) - if process.returncode != 0: + return_code = process.wait() + await self._emit_progress(f"Manim process finished with exit code {return_code}.", raw=True) + if return_code != 0: raise ManimRenderError( trim_error_message( "\n".join(part for part in ["\n".join(stdout_lines), "\n".join(stderr_lines)] if part)