File size: 1,412 Bytes
68d34d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import threading
import time
import weakref

class ModelTimeoutManager:
    def __init__(self, timeout=60):
        self.timeout = timeout
        self.models = {}
        self.lock = threading.Lock()
        self.check_thread = threading.Thread(target=self._check_timeout)
        self.check_thread.daemon = True
        self.check_thread.start()

    def set_model(self, model_name, model):
        with self.lock:
            self.models[model_name] = {
                "model": weakref.ref(model),
                "last_access_time": time.time()
            }

    def get_model(self, model_name):
        with self.lock:
            if model_name in self.models:
                self.models[model_name]["last_access_time"] = time.time()
                return self.models[model_name]["model"]()
            return None

    def _check_timeout(self):
        while True:
            time.sleep(10)
            with self.lock:
                current_time = time.time()
                for model_name in list(self.models.keys()):
                    if current_time - self.models[model_name]["last_access_time"] > self.timeout:
                        model = self.models[model_name]["model"]()
                        if model is not None:
                            del model
                        del self.models[model_name]
                        print(f"Model '{model_name}' released due to timeout")