Heuristic:869413421 Ai moive studio Concurrency Semaphore Tip
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Concurrency, Rate_Limiting |
| Last Updated | 2026-10-23 14:00 GMT |
Overview
Use asyncio.Semaphore to cap concurrent API calls and FFmpeg subprocesses during batch generation, preventing provider rate limits and resource exhaustion.
Description
All batch generation services in the platform use asyncio.Semaphore to limit how many tasks run concurrently within an asyncio.gather call. The semaphore value varies by task type: image generation uses 20 (API-bounded), video synthesis uses 3 (FFmpeg CPU-bounded), transition video generation uses 5 (API-bounded), and keyframe generation uses scene-level concurrency of 3 with per-scene API concurrency of 20. The semaphore is acquired inside the worker function, not at the gather level.
Usage
Use this heuristic whenever running batch operations via asyncio.gather that call external APIs or spawn subprocesses. The semaphore prevents:
- Provider 429 rate-limit errors (image: 20, audio: 20, video: 5)
- CPU/memory exhaustion from concurrent FFmpeg processes (video synthesis: 3)
- Database connection pool exhaustion
The Insight (Rule of Thumb)
- Action: Create an asyncio.Semaphore(N) and acquire it inside each worker via async with semaphore:.
- Value: Image generation: N=20; Video synthesis (FFmpeg): N=3; Transition video: N=5; Scene-level keyframe: N=3 (outer) + N=20 (inner API).
- Trade-off: Lower N = safer but slower; higher N = faster but risks rate limits and resource exhaustion.
- Compatibility: Works with any async function; semaphore is acquired per-worker, not per-gather.
Reasoning
External AI APIs enforce rate limits (typically 20-30 concurrent requests per key). FFmpeg subprocesses each consume significant CPU and memory. Without a semaphore, asyncio.gather fires all tasks simultaneously, causing:
- 429 rate-limit errors (API tasks) — mitigated by Heuristic:869413421_Ai_moive_studio_Retry_With_Backoff_Tip but better prevented.
- CPU/memory exhaustion (FFmpeg tasks) — can crash the worker process.
- Database connection pool exhaustion (all workers open sessions simultaneously).
The semaphore pattern with async with ensures the limit is released even on exceptions.
Code Evidence
Image service semaphore (20) from `image.py:127`:
semaphore = asyncio.Semaphore(20)
tasks = [
process_sentence(sentence, llm_provider, semaphore, storage_client, user_id, model)
for sentence in sentences
]
results = await asyncio.gather(*tasks, return_exceptions=True)
Video synthesis semaphore (3) from `video_synthesis.py:423`:
semaphore = asyncio.Semaphore(3) # 限制并发数为3
tasks_list = [
self._process_sentence_with_cache(
sentence, temp_dir, idx, gen_setting, semaphore, str(task.user_id), api_key, model
)
for idx, sentence in enumerate(sentences_to_generate)
]
results = await asyncio.gather(*tasks_list, return_exceptions=True)
Transition video semaphore (5) from `transition_service.py:543-544`:
max_concurrent = 5
semaphore = asyncio.Semaphore(max_concurrent)
Keyframe dual-level semaphores from `visual_identity_service.py:283,350`:
worker_semaphore = asyncio.Semaphore(20) # API调用并发限制
scene_semaphore = asyncio.Semaphore(3) # 场景并发限制
Related Pages
- Implementation:869413421_Ai_moive_studio_ImageService_generate_images
- Implementation:869413421_Ai_moive_studio_VideoSynthesisService_synthesize_video
- Implementation:869413421_Ai_moive_studio_TransitionService_batch_generate_transition_videos
- Implementation:869413421_Ai_moive_studio_VisualIdentityService_batch_generate_keyframes
- Implementation:869413421_Ai_moive_studio_SceneImageService_batch_generate_scene_images