Skip to content

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

perf(state): copy only requested memory task page

Rudy Celekliee3f7afaf93c
diff --git a/app/services/state.py b/app/services/state.py
index 7ee25c22..7b629593 100644
--- a/app/services/state.py
+++ b/app/services/state.py
@@ -2,6 +2,7 @@ import ast
import copy
import threading
from abc import ABC, abstractmethod
+from itertools import islice
from app.config import config
from app.models import const
@@ -55,9 +56,12 @@ class MemoryState(BaseState):
start = (page - 1) * page_size
end = start + page_size
with self._lock:
- tasks = [copy.deepcopy(task) for task in self._tasks.values()]
- total = len(tasks)
- return tasks[start:end], total
+ total = len(self._tasks)
+ tasks = [
+ copy.deepcopy(task)
+ for task in islice(self._tasks.values(), start, end)
+ ]
+ return tasks, total
def list_task_ids(self, scan_count: int = 100) -> list[str]:
with self._lock:
diff --git a/test/services/test_state.py b/test/services/test_state.py
index 67f8c935..b8daa4e2 100644
--- a/test/services/test_state.py
+++ b/test/services/test_state.py
@@ -129,6 +129,32 @@ class TestMemoryState(unittest.TestCase):
self.assertEqual(total, 1)
self.assertEqual(state.get_task("task-1")["videos"], ["first.mp4"])
+ def test_get_all_tasks_copies_only_the_requested_page(self):
+ """A small page should not clone every historical task payload."""
+
+ class CopyTracked:
+ def __init__(self):
+ self.copies = 0
+
+ def __deepcopy__(self, memo):
+ self.copies += 1
+ return CopyTracked()
+
+ state = MemoryState()
+ off_page = CopyTracked()
+ state.update_task("task-1", videos=["first.mp4"])
+ state.update_task("task-2", payload=off_page)
+
+ first_page, total = state.get_all_tasks(page=1, page_size=1)
+
+ self.assertEqual(total, 2)
+ self.assertEqual([task["task_id"] for task in first_page], ["task-1"])
+ self.assertEqual(off_page.copies, 0)
+
+ second_page, _ = state.get_all_tasks(page=2, page_size=1)
+ self.assertEqual([task["task_id"] for task in second_page], ["task-2"])
+ self.assertEqual(off_page.copies, 1)
+
def test_concurrent_memory_updates_are_preserved(self):
state = MemoryState()
thread_count = 5