Keep native Qwen TTS resident after cooperative stream cancellation

This commit is contained in:
Mikei386 committed 2026-10-19 12:03:47 +02:00
1 parent 3435379ce0
commit 182c8a1e8f
6 files changed
+98 -14

No files matched your search

+14 -4
View File
@@ -5,6 +5,7 @@ from tts_test import TTSTests
from pocket_runtime import supported,TOKENIZER_REVISION
class Pocket(TTSTests):
name='Pocket'
cooperative_cancel=False
def __init__(self,*args):
super().__init__(*args);self.gate=threading.Lock();self.session_key=None
def voice_path(self,name):
@@ -72,17 +73,26 @@ class Pocket(TTSTests):
return dict(self.job)
except Exception:self.gate.release();raise
def _run_cpu(self,p,ident,text,voice,streaming):
directory=self.root/ident;state='failed';phase=self.name+'-Spracherzeugung fehlgeschlagen.'
directory=self.root/ident;state='failed';phase=self.name+'-Spracherzeugung fehlgeschlagen.';cancel_reusable=False
try:
process=self._ensure_cpu(p)
if self.cancel.is_set():raise InterruptedError()
if self.cancel.is_set():
cancel_reusable=self.cooperative_cancel and process.poll() is None
raise InterruptedError()
process.stdin.write(json.dumps(dict(output=str(directory),text=text,voice=str(voice),streaming=streaming))+'\n');process.stdin.flush()
end=time.monotonic()+600
while not (directory/'result.json').is_file():
if self.cancel.wait(.05):raise InterruptedError()
if self.cooperative_cancel:
if self.cancel.is_set():(directory/'cancel').touch()
time.sleep(.05)
elif self.cancel.wait(.05):raise InterruptedError()
if process.poll() is not None:raise ValueError(self.name+'-CPU-Worker wurde beendet. RAM und Laufzeit prüfen.')
if time.monotonic()>end:raise ValueError(self.name+'-Zeitlimit überschritten.')
result=json.loads((directory/'result.json').read_text())
if self.cooperative_cancel and (result.get('cancelled') or self.cancel.is_set()):
cancel_reusable=bool(result.get('ok') and process.poll() is None)
if not cancel_reusable:raise ValueError(result.get('error',self.name+'-Abbruch konnte nicht bestätigt werden.'))
raise InterruptedError()
if not result.get('ok'):raise ValueError(result.get('error',self.name+' fehlgeschlagen.'))
if self.cancel.is_set():raise InterruptedError()
if not 0<(directory/'result.wav').stat().st_size<=32*1024**2:raise ValueError('Ungültige Pocket-Audioausgabe.')
@@ -91,7 +101,7 @@ class Pocket(TTSTests):
except InterruptedError:state='cancelled';phase=self.name+' abgebrochen.'
except Exception as exc:phase=str(exc) if isinstance(exc,ValueError) else self.name+'-Ausführung fehlgeschlagen ('+type(exc).__name__+').'
finally:
if state!='complete' or self.policy=='per_request':self.unload_idle()
if self.policy=='per_request' or state!='complete' and not (state=='cancelled' and cancel_reusable):self.unload_idle()
if state=='complete':(directory/'complete').touch()
else:
for f in directory.glob('chunk-*.pcm'):f.unlink(missing_ok=True)