DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
refactor: use ProactorEventLoop policy instead of subprocess.run fallback
Bingxi Zhao (Frank)b750f5b19f27
diff --git a/deeptutor/agents/math_animator/renderer.py b/deeptutor/agents/math_animator/renderer.pyindex 797064abe..32c06dd22 100644--- a/deeptutor/agents/math_animator/renderer.py+++ b/deeptutor/agents/math_animator/renderer.py@@ -4,7 +4,6 @@ from __future__ import annotationsimport asyncioimport re-import subprocessimport sysfrom pathlib import Pathfrom typing import Awaitable, Callable@@ -146,32 +145,43 @@ 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.run in a thread to avoid asyncio event loop- # compatibility issues on Windows (SelectorEventLoop does not- # support create_subprocess_exec).- def _run_sync() -> subprocess.CompletedProcess:- return subprocess.run(- command,- capture_output=True,- text=True,- )-- result = await asyncio.to_thread(_run_sync)-- stdout_lines = [l for l in (result.stdout or "").splitlines() if l.strip()]- stderr_lines = [l for l in (result.stderr or "").splitlines() if l.strip()]- for line in stdout_lines:- await self._emit_progress(f"[stdout] {line}", raw=True)- for line in stderr_lines:- await self._emit_progress(f"[stderr] {line}", raw=True)-- await self._emit_progress(f"Manim process finished with exit code {result.returncode}.", raw=True)- if result.returncode != 0:+ stdout_lines: list[str] = []+ stderr_lines: list[str] = []++ 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:raise ManimRenderError(trim_error_message("\n".join(part for part in ["\n".join(stdout_lines), "\n".join(stderr_lines)] if part)diff --git a/deeptutor/api/run_server.py b/deeptutor/api/run_server.pyindex e867d015b..17a40e02d 100644--- a/deeptutor/api/run_server.py+++ b/deeptutor/api/run_server.py@@ -4,10 +4,17 @@ Uvicorn Server Startup ScriptUses Python API instead of command line to avoid Windows path parsing issues."""+import asyncioimport osfrom pathlib import Pathimport sys+# Windows: uvicorn defaults to SelectorEventLoop which does not support+# asyncio.create_subprocess_exec. Switch to ProactorEventLoop so that+# child-process APIs (used by Math Animator renderer, etc.) work correctly.+if sys.platform == "win32":+ asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())+import uvicorn# Force unbuffered outputdiff --git a/deeptutor_cli/main.py b/deeptutor_cli/main.pyindex 15427d40b..392de636e 100644--- a/deeptutor_cli/main.py+++ b/deeptutor_cli/main.py@@ -97,7 +97,17 @@ def serve(reload: bool = typer.Option(False, help="Enable auto-reload for development."),) -> None:"""Start the DeepTutor API server."""+ import asyncio+ import sys+set_mode(RunMode.SERVER)++ # Windows: uvicorn defaults to SelectorEventLoop which does not support+ # asyncio.create_subprocess_exec. Switch to ProactorEventLoop so that+ # child-process APIs (used by Math Animator renderer, etc.) work correctly.+ if sys.platform == "win32":+ asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())+try:import uvicornexcept ImportError: