From 65f9cd8d615c30944430a5bde8f1fe5d98636542 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sun, 18 May 2025 17:59:02 +0200 Subject: [PATCH] moved the creation of the thread to the subject instead of the request handler --- src/routes/answers.py | 3 +-- src/utils/observer.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/routes/answers.py b/src/routes/answers.py index 6145e8f..a3ff8bc 100644 --- a/src/routes/answers.py +++ b/src/routes/answers.py @@ -99,8 +99,7 @@ def submit_refinement(): socket2observer[sid] = obs subject.registerObserver(obs) - t = Thread(target=subject.launch_task, args=(validated,), daemon=True) - t.start() + subject.launch_task(validated) url = url_for(f".status", id=process_id, _external=True) return jsonify( { diff --git a/src/utils/observer.py b/src/utils/observer.py index 8d88c27..6529701 100644 --- a/src/utils/observer.py +++ b/src/utils/observer.py @@ -1,6 +1,7 @@ from abc import ABC, abstractmethod from enum import Enum -from typing import Callable, Optional, Set, Any +from threading import Thread +from typing import Callable, Iterable, Mapping, Optional, Set, Any class Status(Enum): @@ -61,9 +62,17 @@ class Subject: def launch_task(self, *args, **kwargs): self.status = Status.PROCESSING - self.task( - *args, **kwargs, percent_cb=self.notifyPercentage, complete_cb=self.notifyComplete + t = Thread( + target=self.task, + args=args, + kwargs={ + **kwargs, + "percent_cb": self.notifyPercentage, + "complete_cb": self.notifyComplete, + }, + daemon=True, ) + t.start() request2status: dict[str, Subject] = {}