Spaces:
Runtime error
Runtime error
Delete server/process_manager.py
Browse files- server/process_manager.py +0 -67
server/process_manager.py
DELETED
|
@@ -1,67 +0,0 @@
|
|
| 1 |
-
"""Управление потоками и отмена генерации"""
|
| 2 |
-
|
| 3 |
-
import threading
|
| 4 |
-
from typing import List, Dict
|
| 5 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 6 |
-
|
| 7 |
-
class ProcessManager:
|
| 8 |
-
def __init__(self):
|
| 9 |
-
self.active_threads: List[threading.Thread] = []
|
| 10 |
-
self.cancel_flags: Dict[int, bool] = {}
|
| 11 |
-
self.lock = threading.Lock()
|
| 12 |
-
self.executor = ThreadPoolExecutor(max_workers=10)
|
| 13 |
-
self.futures = []
|
| 14 |
-
|
| 15 |
-
def register_thread(self, thread: threading.Thread) -> None:
|
| 16 |
-
with self.lock:
|
| 17 |
-
self.active_threads.append(thread)
|
| 18 |
-
self.cancel_flags[thread.ident] = False
|
| 19 |
-
|
| 20 |
-
def register_future(self, future) -> None:
|
| 21 |
-
with self.lock:
|
| 22 |
-
self.futures.append(future)
|
| 23 |
-
|
| 24 |
-
def cancel_all(self) -> str:
|
| 25 |
-
with self.lock:
|
| 26 |
-
for future in self.futures:
|
| 27 |
-
if not future.done():
|
| 28 |
-
future.cancel()
|
| 29 |
-
self.futures.clear()
|
| 30 |
-
for thread_id in self.cancel_flags:
|
| 31 |
-
self.cancel_flags[thread_id] = True
|
| 32 |
-
for thread in self.active_threads:
|
| 33 |
-
if thread.is_alive():
|
| 34 |
-
try:
|
| 35 |
-
thread.join(timeout=0.5)
|
| 36 |
-
except Exception:
|
| 37 |
-
pass
|
| 38 |
-
self.active_threads.clear()
|
| 39 |
-
self.cancel_flags.clear()
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
from interpreter import interpreter
|
| 43 |
-
if hasattr(interpreter, 'cancel'):
|
| 44 |
-
interpreter.cancel()
|
| 45 |
-
except Exception:
|
| 46 |
-
pass
|
| 47 |
-
|
| 48 |
-
from .state import STATE
|
| 49 |
-
STATE.cancel_flag = True
|
| 50 |
-
STATE.current_mode = "paused"
|
| 51 |
-
return "✅ Все процессы генерации остановлены!"
|
| 52 |
-
|
| 53 |
-
def is_cancelled(self, thread_id: int = None) -> bool:
|
| 54 |
-
if thread_id is None:
|
| 55 |
-
thread_id = threading.current_thread().ident
|
| 56 |
-
with self.lock:
|
| 57 |
-
return self.cancel_flags.get(thread_id, False)
|
| 58 |
-
|
| 59 |
-
def clear(self) -> None:
|
| 60 |
-
with self.lock:
|
| 61 |
-
self.active_threads = [t for t in self.active_threads if t.is_alive()]
|
| 62 |
-
|
| 63 |
-
def get_active_count(self) -> int:
|
| 64 |
-
with self.lock:
|
| 65 |
-
return len([t for t in self.active_threads if t.is_alive()])
|
| 66 |
-
|
| 67 |
-
PROCESS_MANAGER = ProcessManager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|