Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(webui): show task logs written by helper threads

amitk2108bce8e49ec959
diff --git a/app/services/material.py b/app/services/material.py
index fdc10a17..adda5f1a 100644
--- a/app/services/material.py
+++ b/app/services/material.py
@@ -29,7 +29,7 @@ from app.services import (
video,
volcengine_seedance,
)
-from app.utils import utils
+from app.utils import logging_utils, utils
# Thread-safe counter for API key rotation
_api_key_counter = 0
@@ -1894,7 +1894,7 @@ def _search_terms_in_parallel(
) as executor:
for search_term in search_terms:
futures[executor.submit(
- search_videos,
+ logging_utils.bind_log_scope(search_videos),
search_term,
minimum_duration,
video_aspect,
@@ -1953,7 +1953,7 @@ def _download_materials_in_parallel(
) as executor:
for search_term, item in materials:
futures[executor.submit(
- save_video,
+ logging_utils.bind_log_scope(save_video),
item.url,
material_directory,
)] = (search_term, item)
diff --git a/app/services/video.py b/app/services/video.py
index 18ca9247..c9a53652 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -41,7 +41,7 @@ from app.models.schema import (
)
from app.services import bgm as bgm_service
from app.services.utils import video_effects
-from app.utils import file_security, utils
+from app.utils import file_security, logging_utils, utils
class SubClippedVideoClip:
def __init__(
@@ -514,7 +514,9 @@ def _run_concat_with_heartbeat(command: list[str], output_file: str):
f"{_describe_concat_output_progress(output_file)}"
)
- reporter = threading.Thread(target=log_heartbeat, daemon=True)
+ reporter = threading.Thread(
+ target=logging_utils.bind_log_scope(log_heartbeat), daemon=True
+ )
reporter.start()
try:
configured_timeout = config.app.get(
@@ -1060,6 +1062,9 @@ def combine_videos(
if clip_file:
delete_files(clip_file)
+ # 片段始终在线程池里处理。逐片段日志是这一阶段唯一的进度信息,绑定到
+ # 发起合成的线程后,WebUI 的任务日志才能收集到它们。
+ process_clip_in_task_scope = logging_utils.bind_log_scope(process_one_clip)
clip_processing_workers = 1
if len(subclipped_items) >= 2:
clip_processing_workers = min(_get_clip_processing_concurrency(), len(subclipped_items))
@@ -1088,7 +1093,7 @@ def combine_videos(
candidate_index += 1
if not batch:
break
- for processed_clip in executor.map(process_one_clip, batch):
+ for processed_clip in executor.map(process_clip_in_task_scope, batch):
if processed_clip is None:
continue
processed_clips.append(processed_clip)
diff --git a/app/services/webui_task.py b/app/services/webui_task.py
index f3c10fb4..690a7571 100644
--- a/app/services/webui_task.py
+++ b/app/services/webui_task.py
@@ -10,7 +10,7 @@ from app.models.schema import VideoParams
from app.services import state as sm
from app.services import task as tm
from app.services.loomloom import LoomLoomConfirmedVideoRequest
-from app.utils.logging_utils import format_log_record
+from app.utils.logging_utils import format_log_record, log_scope_thread_id
# WebUI 的配置保存在进程级全局字典中。原来的同步实现会在完整生成期间持有
@@ -64,7 +64,9 @@ def _run_generation(
在后台线程中执行现有视频流水线。
Loguru 的 sink 是进程级资源,因此必须按当前工作线程过滤。否则同时运行的
- API 任务或其它页面日志会混入当前任务。页面只读取普通列表快照,不会从后台
+ API 任务或其它页面日志会混入当前任务。任务为下载、片段编码和心跳启动的
+ 辅助线程通过 bind_log_scope 归属到工作线程,它们的日志同样要收集,否则
+ 这些耗时阶段在 WebUI 里没有任何输出。页面只读取普通列表快照,不会从后台
线程访问 Streamlit session_state,从根源上避免刷新时的 delta 路径错乱。
"""
log_handler_id = None
@@ -76,7 +78,9 @@ def _run_generation(
level="DEBUG",
format=format_log_record,
colorize=False,
- filter=lambda record: record["thread"].id == worker_thread_id,
+ filter=lambda record: (
+ log_scope_thread_id(record["thread"].id) == worker_thread_id
+ ),
)
# 完整任务仍使用原来的配置锁,防止另一个 WebUI 会话在生成中途修改
diff --git a/app/utils/logging_utils.py b/app/utils/logging_utils.py
index 82e06ba3..85cb635d 100644
--- a/app/utils/logging_utils.py
+++ b/app/utils/logging_utils.py
@@ -1,3 +1,4 @@
+import functools
import os
import threading
@@ -18,6 +19,53 @@ LOG_RECORD_FORMAT = (
# 用于收集 WebUI 日志的临时 sink 也会被删除。
_terminal_handler_id: int | None = 0
_terminal_handler_lock = threading.RLock()
+# WebUI 按任务工作线程的 ID 过滤日志。任务为并行下载、片段编码和心跳启动的
+# 辅助线程有各自的线程 ID,它们写出的日志会被整条丢弃,结果耗时最长的阶段
+# 在 WebUI 里反而没有任何输出。这里记录“辅助线程 -> 发起它的线程”的映射,
+# 让日志过滤器可以把辅助线程的记录归回所属任务。
+_log_scope_roots: dict[int, int] = {}
+_log_scope_lock = threading.Lock()
+
+
+def log_scope_thread_id(thread_id: int | None = None) -> int:
+ """返回线程所属日志作用域的根线程 ID;未绑定的线程就是它自己。"""
+ if thread_id is None:
+ thread_id = threading.get_ident()
+ with _log_scope_lock:
+ return _log_scope_roots.get(thread_id, thread_id)
+
+
+def bind_log_scope(func):
+ """
+ 包装一个将在其它线程执行的函数,使其日志归属调用本函数的线程。
+
+ 必须在提交任务的线程里调用:作用域在包装时确定,辅助线程再启动的线程
+ 也会归到最初的任务线程。执行结束后解除绑定,因为线程 ID 会被系统复用,
+ 残留映射会把之后无关线程的日志算进已经结束的任务。
+ """
+ root_thread_id = log_scope_thread_id()
+
+ @functools.wraps(func)
+ def run_in_log_scope(*args, **kwargs):
+ thread_id = threading.get_ident()
+ if thread_id == root_thread_id:
+ return func(*args, **kwargs)
+
+ with _log_scope_lock:
+ previous_root = _log_scope_roots.get(thread_id)
+ _log_scope_roots[thread_id] = root_thread_id
+ try:
+ return func(*args, **kwargs)
+ finally:
+ with _log_scope_lock:
+ # 线程池会复用线程:恢复进入前的归属,而不是一律删除,避免
+ # 嵌套包装提前清掉外层仍在使用的绑定。
+ if previous_root is None:
+ _log_scope_roots.pop(thread_id, None)
+ else:
+ _log_scope_roots[thread_id] = previous_root
+
+ return run_in_log_scope
def _project_relative_path(file_path):
diff --git a/test/services/test_material.py b/test/services/test_material.py
index b74082f5..740f4d7b 100644
--- a/test/services/test_material.py
+++ b/test/services/test_material.py
@@ -1,17 +1,39 @@
import os
import sys
import tempfile
+import threading
import unittest
+from contextlib import contextmanager
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
import requests
+from loguru import logger
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from app.config import config
from app.services import material
+from app.utils import logging_utils
+
+
+@contextmanager
+def _capture_task_scoped_logs():
+ """按 WebUI 任务日志的同一条规则收集日志:只保留归属当前线程的记录。"""
+ messages = []
+ root_thread_id = threading.get_ident()
+ handler_id = logger.add(
+ lambda message: messages.append(message.record["message"]),
+ level="DEBUG",
+ filter=lambda record: (
+ logging_utils.log_scope_thread_id(record["thread"].id) == root_thread_id
+ ),
+ )
+ try:
+ yield messages
+ finally:
+ logger.remove(handler_id)
class _FakeVideoDownloadResponse:
@@ -1414,6 +1436,41 @@ class TestMaterialTlsVerification(unittest.TestCase):
# 与串行逻辑的下载集合一致:{a, c},d 不会被多下载。
self.assertEqual(result, ["/tmp/a.mp4", "/tmp/c.mp4"])
+ def test_parallel_download_logs_belong_to_the_task_log_scope(self):
+ """
+ 并发数大于 1 时素材在 material-download 线程池里下载。这些线程写出的
+ 日志必须归属发起下载的任务线程,否则调高并发后 WebUI 反而看不到下载
+ 过程中的任何输出。
+ """
+ items = [
+ material.MaterialInfo(
+ provider="pexels",
+ url=f"https://v.example/{name}.mp4",
+ duration=6,
+ )
+ for name in ("a", "b")
+ ]
+
+ def fake_save_video(video_url, save_dir=""):
+ name = video_url.rsplit("/", 1)[-1]
+ logger.info(f"worker downloading {name}")
+ return f"/tmp/{name}"
+
+ with (
+ patch.dict(config.app, {"material_concurrency": 4}),
+ patch.object(material, "save_video", side_effect=fake_save_video),
+ _capture_task_scoped_logs() as messages,
+ ):
+ material._download_materials_in_parallel(
+ materials=[("city", item) for item in items],
+ material_directory="",
+ )
+
+ self.assertEqual(
+ sorted(m for m in messages if m.startswith("worker downloading")),
+ ["worker downloading a.mp4", "worker downloading b.mp4"],
+ )
+
def test_material_concurrency_is_clamped(self):
"""素材并发配置钳制在 1~8,非法值回退到串行默认值。"""
with patch.dict(config.app, {}, clear=True):
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 04f56786..fe2f1c85 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -3,14 +3,16 @@ import shutil
import subprocess
import sys
import tempfile
+import threading
import time
import types
import unittest
-from contextlib import redirect_stdout
+from contextlib import contextmanager, redirect_stdout
from io import StringIO
from pathlib import Path
from unittest.mock import patch
+from loguru import logger
from moviepy import (
ImageClip,
VideoFileClip,
@@ -22,11 +24,34 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from app.config import config
from app.models.schema import MaterialInfo
from app.services import video as vd
-from app.utils import utils
+from app.utils import logging_utils, utils
resources_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources")
+@contextmanager
+def _capture_task_scoped_logs():
+ """
+ 按 WebUI 任务日志的同一条规则收集日志:只保留归属当前线程的记录。
+
+ 直接 patch ``logger.info`` 无法区分日志来自哪个线程,而 WebUI 丢日志的
+ 原因正是线程归属,所以这里用真实的 loguru sink 加作用域过滤来验证。
+ """
+ messages = []
+ root_thread_id = threading.get_ident()
+ handler_id = logger.add(
+ lambda message: messages.append(message.record["message"]),
+ level="DEBUG",
+ filter=lambda record: (
+ logging_utils.log_scope_thread_id(record["thread"].id) == root_thread_id
+ ),
+ )
+ try:
+ yield messages
+ finally:
+ logger.remove(handler_id)
+
+
class _FakeMoviePyClip:
"""为最终混音单测提供最小 MoviePy 接口,避免 CI 真实编码大型视频。"""
@@ -1402,6 +1427,90 @@ class TestVideoService(unittest.TestCase):
self.assertTrue(heartbeats, "耗时拼接期间必须记录存活日志")
self.assertRegex(heartbeats[0], r"elapsed=\d+s, output size: 0\.00 MB")
+ def test_concat_heartbeat_belongs_to_the_task_log_scope(self):
+ """
+ 心跳由独立线程写出。WebUI 只收集任务线程作用域内的日志,心跳线程不
+ 绑定作用域时,终端能看到存活信息,WebUI 面板却依旧一片空白。
+ """
+
+ def slow_run(command, capture_output, text, check, **kwargs):
+ time.sleep(0.2)
+ Path(command[-1]).write_bytes(b"encoded-video")
+ return types.SimpleNamespace(returncode=0, stdout="", stderr="")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ clip_file = os.path.join(temp_dir, "clip.mp4")
+ output_file = os.path.join(temp_dir, "combined.mp4")
+ Path(clip_file).write_bytes(b"fake")
+
+ with (
+ patch.object(vd, "_FFMPEG_CONCAT_HEARTBEAT_SECONDS", 0.02),
+ patch.object(vd.subprocess, "run", side_effect=slow_run),
+ _capture_task_scoped_logs() as messages,
+ ):
+ vd.concat_video_clips_with_ffmpeg(
+ clip_files=[clip_file],
+ output_file=output_file,
+ threads=1,
+ output_dir=temp_dir,
+ )
+
+ self.assertTrue(
+ [message for message in messages if "still running" in message],
+ "心跳日志必须归属发起拼接的任务线程",
+ )
+
+ def test_clip_processing_logs_belong_to_the_task_log_scope(self):
+ """
+ 片段始终在 clip-process 线程池里处理,即使并发数为 1。逐片段日志是这一
+ 阶段唯一的进度信息,必须归属任务线程,WebUI 才能显示“正在处理第几段”。
+ """
+
+ class _FakeAudioClip:
+ duration = 4.0
+
+ def close(self):
+ pass
+
+ class _FakeVideoClip:
+ def __init__(self, duration):
+ self.duration = duration
+ self.size = (1080, 1920)
+ self.w = 1080
+ self.h = 1920
+
+ def subclipped(self, start_time, end_time):
+ return _FakeVideoClip(end_time - start_time)
+
+ def close(self):
+ pass
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch.object(vd, "AudioFileClip", return_value=_FakeAudioClip()),
+ patch.object(
+ vd,
+ "_open_video_clip_quietly",
+ side_effect=lambda _path: _FakeVideoClip(10.0),
+ ),
+ patch.object(vd, "_write_videofile_with_codec_fallback"),
+ patch.object(vd, "concat_video_clips_with_ffmpeg"),
+ patch.object(vd, "delete_files"),
+ _capture_task_scoped_logs() as messages,
+ ):
+ vd.combine_videos(
+ combined_video_path=os.path.join(temp_dir, "combined.mp4"),
+ video_paths=["clip.mp4"],
+ audio_file="audio.mp3",
+ video_concat_mode=vd.VideoConcatMode.sequential,
+ max_clip_duration=2,
+ )
+
+ self.assertTrue(
+ [message for message in messages if message.startswith("processing clip")],
+ "逐片段处理日志必须归属发起合成的任务线程",
+ )
+
def test_concat_timeout_fails_without_retrying_another_codec(self):
"""A stalled FFmpeg must fail the task and release the concat list file."""
config.app["ffmpeg_concat_timeout_seconds"] = 12
diff --git a/test/services/test_webui_task.py b/test/services/test_webui_task.py
index 816fce17..a72c20ae 100644
--- a/test/services/test_webui_task.py
+++ b/test/services/test_webui_task.py
@@ -489,6 +489,91 @@ def test_worker_logs_are_available_without_streamlit_session_state():
)
+def test_bound_helper_thread_logs_reach_the_task_log():
+ """
+ 并行下载、片段编码和 ffmpeg 心跳都在任务启动的辅助线程里写日志。只按工作
+ 线程 ID 过滤时这些记录会被丢弃,WebUI 在耗时最长的阶段反而没有任何输出。
+ 经 bind_log_scope 绑定的辅助线程必须计入所属任务,未绑定的线程仍然排除,
+ 否则同时运行的 API 任务日志会混进来。
+ """
+ task_id = "helper-thread-log-test"
+ with webui_task._task_logs_lock:
+ webui_task._task_logs.pop(task_id, None)
+
+ def logged_start(**_kwargs):
+ bound = threading.Thread(
+ target=logging_utils.bind_log_scope(
+ lambda: logger.info("bound helper thread log")
+ )
+ )
+ unrelated = threading.Thread(
+ target=lambda: logger.info("unrelated thread log")
+ )
+ for thread in (bound, unrelated):
+ thread.start()
+ for thread in (bound, unrelated):
+ thread.join()
+ return {"videos": []}
+
+ with (
+ patch.object(webui_task.tm, "start", side_effect=logged_start),
+ patch.object(
+ webui_task.config,
+ "runtime_config_lock",
+ return_value=nullcontext(),
+ ),
+ ):
+ webui_task._run_generation(
+ task_id,
+ VideoParams(video_subject="辅助线程日志"),
+ capture_logs=True,
+ )
+
+ messages = "\n".join(webui_task.get_task_logs(task_id))
+ assert "bound helper thread log" in messages
+ assert "unrelated thread log" not in messages
+
+
+def test_bind_log_scope_follows_nested_helpers_and_is_released():
+ """
+ 辅助线程再启动的线程(例如并行片段里的心跳)也要归属最初的任务线程。
+ 线程结束后必须解除绑定:线程 ID 会被系统复用,残留的映射会把之后无关
+ 线程的日志错误地算进旧任务。
+ """
+ root_thread_id = threading.get_ident()
+ seen = {}
+
+ def inner():
+ seen["inner_scope"] = logging_utils.log_scope_thread_id()
+
+ def outer():
+ seen["outer_thread_id"] = threading.get_ident()
+ seen["outer_scope"] = logging_utils.log_scope_thread_id()
+ nested = threading.Thread(target=logging_utils.bind_log_scope(inner))
+ nested.start()
+ nested.join()
+
+ helper = threading.Thread(target=logging_utils.bind_log_scope(outer))
+ helper.start()
+ helper.join()
+
+ assert seen["outer_scope"] == root_thread_id
+ assert seen["inner_scope"] == root_thread_id
+ assert (
+ logging_utils.log_scope_thread_id(seen["outer_thread_id"])
+ == seen["outer_thread_id"]
+ )
+ assert logging_utils.log_scope_thread_id() == root_thread_id
+
+
+def test_bind_log_scope_runs_inline_calls_without_rebinding():
+ """同一线程内直接调用包装后的函数时保持原样返回值,不改动作用域。"""
+ bound = logging_utils.bind_log_scope(lambda value: value * 2)
+
+ assert bound(21) == 42
+ assert logging_utils.log_scope_thread_id() == threading.get_ident()
+
+
def test_webui_worker_forwards_reference_audio_to_pipeline():
reference_audio = b"task-local-reference-wav"
prompt_audio = b"task-local-prompt-wav"