From 3c4244597cb58b15af9670e2901cc445cf5abef1 Mon Sep 17 00:00:00 2001 From: Admin MPCZ Date: Wed, 15 Apr 2026 00:20:12 +0200 Subject: [PATCH] Audit: ThreadPoolExecutor avec parallel borne (evite saturation DB/PSMP) --- app/routers/audit.py | 4 ++-- app/services/realtime_audit_service.py | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/app/routers/audit.py b/app/routers/audit.py index f8e180b..78c3a6d 100644 --- a/app/routers/audit.py +++ b/app/routers/audit.py @@ -181,8 +181,8 @@ async def audit_global(request: Request, db=Depends(get_db)): if not hostnames: return RedirectResponse(url="/audit?msg=no_hosts", status_code=303) - # Lancer en arrière-plan - job_id = start_audit_job(hostnames) + # Lancer en arrière-plan avec parallelisme configure + job_id = start_audit_job(hostnames, parallel=parallel) return RedirectResponse(url=f"/audit/realtime/progress/{job_id}", status_code=303) diff --git a/app/services/realtime_audit_service.py b/app/services/realtime_audit_service.py index 23ee232..fed5824 100644 --- a/app/services/realtime_audit_service.py +++ b/app/services/realtime_audit_service.py @@ -323,8 +323,9 @@ import time as _time _audit_jobs = {} -def start_audit_job(hostnames): - """Lance un audit en arriere-plan. Retourne le job_id.""" +def start_audit_job(hostnames, parallel=3): + """Lance un audit en arriere-plan avec pool de threads borne. Retourne le job_id.""" + from concurrent.futures import ThreadPoolExecutor job_id = str(uuid.uuid4())[:8] job = { "id": job_id, @@ -334,19 +335,16 @@ def start_audit_job(hostnames): "servers": {}, "results": [], "finished": False, + "parallel": parallel, } for hn in hostnames: job["servers"][hn] = {"hostname": hn, "stage": "pending", "detail": "En attente", "status": None} _audit_jobs[job_id] = job def _run(): - threads = [] - for hn in hostnames: - t = threading.Thread(target=_audit_one, args=(job, hn.strip()), daemon=True) - threads.append(t) - t.start() - for t in threads: - t.join() + with ThreadPoolExecutor(max_workers=max(1, int(parallel))) as pool: + for hn in hostnames: + pool.submit(_audit_one, job, hn.strip()) job["finished"] = True job["finished_at"] = _time.time()