close subproc env on shutdown

This commit is contained in:
Timothy Pogue 2022-09-28 03:06:05 +00:00
parent 647200e8a7
commit caa47a2f47
2 changed files with 22 additions and 1 deletions

View File

@ -158,6 +158,13 @@ class IFreqaiModel(ABC):
self.model = None
self.dk = None
def _on_stop(self):
"""
Callback for Subclasses to override to include logic for shutting down resources
when SIGINT is sent.
"""
return
def shutdown(self):
"""
Cleans up threads on Shutdown, set stop event. Join threads to wait
@ -166,6 +173,8 @@ class IFreqaiModel(ABC):
logger.info("Stopping FreqAI")
self._stop_event.set()
self._on_stop()
logger.info("Waiting on Training iteration")
for _thread in self._threads:
_thread.join()

View File

@ -73,7 +73,7 @@ class ReinforcementLearner_multiproc(BaseReinforcementLearningModel):
test_df = data_dictionary["test_features"]
env_id = "train_env"
num_cpu = int(self.freqai_info["rl_config"]["thread_count"])
num_cpu = int(self.freqai_info["rl_config"].get("cpu_count", 2))
self.train_env = SubprocVecEnv([make_env(self.MyRLEnv, env_id, i, 1, train_df, prices_train,
self.reward_params, self.CONV_WIDTH, monitor=True,
config=self.config) for i
@ -88,3 +88,15 @@ class ReinforcementLearner_multiproc(BaseReinforcementLearningModel):
self.eval_callback = EvalCallback(self.eval_env, deterministic=True,
render=False, eval_freq=len(train_df),
best_model_save_path=str(dk.data_path))
def _on_stop(self):
"""
Hook called on bot shutdown. Close SubprocVecEnv subprocesses for clean shutdown.
"""
if hasattr(self, "train_env") and self.train_env:
self.train_env.close()
if hasattr(self, "eval_env") and self.eval_env:
self.eval_env.close()