|
|
|
@@ -1,5 +1,6 @@ |
|
|
|
import os |
|
|
|
import pickle |
|
|
|
import atexit |
|
|
|
import tempfile |
|
|
|
import shortuuid |
|
|
|
from concurrent.futures import ProcessPoolExecutor |
|
|
|
@@ -126,6 +127,12 @@ class LearnwaresContainer: |
|
|
|
) |
|
|
|
for _learnware, _zippath in zip(learnware_list, learnware_zippaths) |
|
|
|
] |
|
|
|
|
|
|
|
model_list = [_learnware.get_model() for _learnware in self.learnware_list] |
|
|
|
with ProcessPoolExecutor(max_workers=max(os.cpu_count() // 2, 1)) as executor: |
|
|
|
executor.map(self._initialize_model_container, model_list) |
|
|
|
|
|
|
|
atexit.register(self.cleanup) |
|
|
|
|
|
|
|
@staticmethod |
|
|
|
def _initialize_model_container(model: ModelEnvContainer): |
|
|
|
@@ -135,16 +142,9 @@ class LearnwaresContainer: |
|
|
|
def _destroy_model_container(model: ModelEnvContainer): |
|
|
|
model.remove_env() |
|
|
|
|
|
|
|
def __enter__(self): |
|
|
|
model_list = [_learnware.get_model() for _learnware in self.learnware_list] |
|
|
|
with ProcessPoolExecutor(max_workers=max(os.cpu_count() // 2, 1)) as executor: |
|
|
|
executor.map(self._initialize_model_container, model_list) |
|
|
|
return self |
|
|
|
|
|
|
|
def __exit__(self, type, value, trace): |
|
|
|
model_list = [_learnware.get_model() for _learnware in self.learnware_list] |
|
|
|
with ProcessPoolExecutor(max_workers=max(os.cpu_count() // 2, 1)) as executor: |
|
|
|
executor.map(self._destroy_model_container, model_list) |
|
|
|
|
|
|
|
def get_learnware_list_with_container(self): |
|
|
|
return self.learnware_list |
|
|
|
|
|
|
|
def cleanup(self): |
|
|
|
for _learnware in self.learnware_list: |
|
|
|
self._destroy_model_container(_learnware.get_model()) |