student-tests: нагрузка останавливается при падении сценария (процесс не зависает)
This commit is contained in:
@@ -13,6 +13,19 @@ from concurrent.futures import ThreadPoolExecutor
|
|||||||
import labutil # noqa: F401 (добавляет tests/ в sys.path)
|
import labutil # noqa: F401 (добавляет tests/ в sys.path)
|
||||||
from client import Client, Failure
|
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:
|
class Load:
|
||||||
def __init__(self, base_url, ca_file, actor, chat, concurrency=4, timeout=10, prefix='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._pool = ThreadPoolExecutor(max_workers=self.concurrency)
|
||||||
self._futures = [self._pool.submit(self._worker, w) for w in range(self.concurrency)]
|
self._futures = [self._pool.submit(self._worker, w) for w in range(self.concurrency)]
|
||||||
self.started = time.time()
|
self.started = time.time()
|
||||||
|
_ACTIVE.append(self)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._stop.set()
|
self._stop.set()
|
||||||
|
if self in _ACTIVE:
|
||||||
|
_ACTIVE.remove(self)
|
||||||
for f in self._futures:
|
for f in self._futures:
|
||||||
f.result()
|
f.result()
|
||||||
self._pool.shutdown()
|
self._pool.shutdown()
|
||||||
|
|||||||
Reference in New Issue
Block a user