student-tests: нагрузка останавливается при падении сценария (процесс не зависает)

This commit is contained in:
2026-11-12 19:28:57 +03:00
parent 049b2867a6
commit 53f6dcedfa
+16
View File
@@ -13,6 +13,19 @@ from concurrent.futures import ThreadPoolExecutor
import labutil # noqa: F401 (добавляет tests/ в sys.path)
from client import Client, Failure
_ACTIVE = []
def _stop_all():
# Если сценарий упал до load.stop(), потоки нагрузки не дадут процессу завершиться
# (ThreadPoolExecutor ждёт их при выходе). Этот hook регистрируется позже
# concurrent.futures и поэтому вызывается раньше его join.
for load in list(_ACTIVE):
load._stop.set()
threading._register_atexit(_stop_all)
class Load:
def __init__(self, base_url, ca_file, actor, chat, concurrency=4, timeout=10, prefix='load'):
@@ -54,10 +67,13 @@ class Load:
self._pool = ThreadPoolExecutor(max_workers=self.concurrency)
self._futures = [self._pool.submit(self._worker, w) for w in range(self.concurrency)]
self.started = time.time()
_ACTIVE.append(self)
return self
def stop(self):
self._stop.set()
if self in _ACTIVE:
_ACTIVE.remove(self)
for f in self._futures:
f.result()
self._pool.shutdown()