GitHub Actions commited on
Commit
57a5b90
·
1 Parent(s): 67272fb

Sync from GitHub repo

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -117,7 +117,8 @@ TTS_CACHE_SIZE = int(os.getenv("TTS_CACHE_SIZE", "10"))
117
  CACHE_AUDIO_SUBDIR = "cache"
118
  tts_cache = {} # sentence -> {model_a, model_b, audio_a, audio_b, created_at}
119
  tts_cache_lock = threading.Lock()
120
- cache_executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix='CacheReplacer')
 
121
  all_harvard_sentences = [] # Keep the full list available
122
 
123
  # Create temp directories
@@ -535,8 +536,21 @@ def generate_tts():
535
  }
536
  app.tts_sessions[session_id] = session_data_from_cache
537
 
538
- # Trigger background task to replace the used cache entry
539
- cache_executor.submit(_generate_cache_entry_task, None) # Pass None to signal replacement
 
 
 
 
 
 
 
 
 
 
 
 
 
540
 
541
  if cache_hit and session_data_from_cache:
542
  # Return response using cached data
 
117
  CACHE_AUDIO_SUBDIR = "cache"
118
  tts_cache = {} # sentence -> {model_a, model_b, audio_a, audio_b, created_at}
119
  tts_cache_lock = threading.Lock()
120
+ # Increased max_workers to 8 for concurrent generation/refill
121
+ cache_executor = ThreadPoolExecutor(max_workers=8, thread_name_prefix='CacheReplacer')
122
  all_harvard_sentences = [] # Keep the full list available
123
 
124
  # Create temp directories
 
536
  }
537
  app.tts_sessions[session_id] = session_data_from_cache
538
 
539
+ # --- Trigger background tasks to refill the cache ---
540
+ # Calculate how many slots need refilling
541
+ current_cache_size = len(tts_cache) # Size *before* adding potentially new items
542
+ needed_refills = TTS_CACHE_SIZE - current_cache_size
543
+ # Limit concurrent refills to 8 or the actual need
544
+ refills_to_submit = min(needed_refills, 8)
545
+
546
+ if refills_to_submit > 0:
547
+ app.logger.info(f"Cache hit: Submitting {refills_to_submit} background task(s) to refill cache (current size: {current_cache_size}, target: {TTS_CACHE_SIZE}).")
548
+ for _ in range(refills_to_submit):
549
+ # Pass None to signal replacement selection within the task
550
+ cache_executor.submit(_generate_cache_entry_task, None)
551
+ else:
552
+ app.logger.info(f"Cache hit: Cache is already full or at target size ({current_cache_size}/{TTS_CACHE_SIZE}). No refill tasks submitted.")
553
+ # --- End Refill Trigger ---
554
 
555
  if cache_hit and session_data_from_cache:
556
  # Return response using cached data