利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
fix(loomloom): isolate staging files for concurrent artifact downloads
Rudy Celekli68eb5a68b93c
diff --git a/app/services/loomloom.py b/app/services/loomloom.pyindex 48d3493c..e4216b61 100644--- a/app/services/loomloom.py+++ b/app/services/loomloom.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport jsonimport mathimport os+import tempfileimport timefrom collections.abc import Callable, Mappingfrom dataclasses import dataclass, field@@ -941,7 +942,7 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):return {**matching[0], "accessUrl": access_url}def _download_video_artifact(self, access_url: str, destination: str) -> None:- temporary = destination + ".part"+ temporary = Nonedownloaded_bytes = 0response = Nonetry:@@ -954,7 +955,11 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):content_length = int(response.headers.get("content-length", 0) or 0)if content_length > MAX_VIDEO_ARTIFACT_BYTES:raise LoomLoomAPIError("video artifact exceeds the download limit")- with open(temporary, "wb") as output:+ with tempfile.NamedTemporaryFile(+ mode="wb", dir=os.path.dirname(destination) or ".",+ prefix=os.path.basename(destination) + ".", suffix=".part", delete=False,+ ) as output:+ temporary = output.namefor chunk in response.iter_content(chunk_size=1024 * 1024):if not chunk:continue@@ -985,5 +990,5 @@ class LoomLoomVideoBackend(LoomLoomScriptBackend):"failed to close LoomLoom video download response: "f"error={type(exc).__name__}")- if os.path.exists(temporary):+ if temporary is not None and os.path.exists(temporary):os.remove(temporary)diff --git a/test/services/test_loom_download_staging_native.py b/test/services/test_loom_download_staging_native.pynew file mode 100644index 00000000..6f0778d2--- /dev/null+++ b/test/services/test_loom_download_staging_native.py@@ -0,0 +1,86 @@+import http.server+import threading+import requests+from app.services.loomloom import LoomLoomVideoBackend, LoomLoomSettings+++def test_native_concurrent_downloads_use_distinct_staging_files(tmp_path):+ first_chunk = threading.Event()+ release = threading.Event()+ size = 1024 * 1024+ payloads = {+ "/first": b"A" * size + b"FIRST-END",+ "/second": b"B" * size + b"SECOND-END",+ }++ class Handler(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ body = payloads[self.path]+ self.send_response(200)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body[:size])+ self.wfile.flush()+ if self.path == "/first":+ first_chunk.set()+ assert release.wait(5)+ self.wfile.write(body[size:])+ self.wfile.flush()++ def log_message(self, *args):+ pass++ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ worker = threading.Thread(target=server.serve_forever, daemon=True)+ worker.start()+ destination = tmp_path / "shared.mp4"+ outcomes = []++ def download(path):+ try:+ with requests.Session() as session:+ backend = LoomLoomVideoBackend(+ LoomLoomSettings(+ base_url="http://127.0.0.1",+ api_token="synthetic",+ market_listing_id="fixture",+ ),+ session=session,+ )+ backend._download_video_artifact(+ f"http://127.0.0.1:{server.server_port}{path}", str(destination)+ )+ outcomes.append("complete")+ except Exception as exc:+ outcomes.append(type(exc).__name__)++ first = threading.Thread(target=download, args=("/first",))+ second = threading.Thread(target=download, args=("/second",))+ try:+ first.start()+ assert first_chunk.wait(5)+ # The first request has already opened its real staging file by the+ # time it waits for the final response chunk.+ import time++ deadline = time.monotonic() + 5+ while not any(tmp_path.glob("*.part")) and time.monotonic() < deadline:+ time.sleep(0.001)+ assert any(tmp_path.glob("*.part"))+ second.start()+ second.join(5)+ assert not second.is_alive()+ release.set()+ first.join(5)+ assert not first.is_alive()+ assert outcomes == ["complete", "complete"]+ assert destination.read_bytes() in payloads.values()+ assert not list(tmp_path.glob("*.part"))+ finally:+ release.set()+ first.join(5)+ if second.ident:+ second.join(5)+ server.shutdown()+ server.server_close()+ worker.join(2)