Skip to content

Commit

Permalink
logging thread safe 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
quantylab committed Mar 12, 2020
1 parent 9cacb40 commit 57c6234
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions learners.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class ReinforcementLearner:
__metaclass__ = abc.ABCMeta
lock = threading.Lock()

def __init__(self, rl_method='rl', stock_code=None,
chart_data=None, training_data=None,
Expand Down Expand Up @@ -215,7 +216,8 @@ def run(
max_trading_unit=self.agent.max_trading_unit,
delayed_reward_threshold=self.agent.delayed_reward_threshold
)
logging.info(info)
with self.lock:
logging.info(info)

# 시작 시간
time_start = time.time()
Expand Down Expand Up @@ -323,17 +325,19 @@ def run(
if self.pos_learning_cnt + self.neg_learning_cnt > 0:
self.loss /= self.pos_learning_cnt \
+ self.neg_learning_cnt
logging.info("[{}][Epoch {}/{}] Epsilon:{:.4f} "
"#Expl.:{}/{} #Buy:{} #Sell:{} #Hold:{} "
"#Stocks:{} PV:{:,.0f} "
"POS:{} NEG:{} Loss:{:.6f} ET:{:.4f}".format(
self.stock_code, epoch_str, num_epoches, epsilon,
self.exploration_cnt, self.itr_cnt,
self.agent.num_buy, self.agent.num_sell,
self.agent.num_hold, self.agent.num_stocks,
self.agent.portfolio_value,
self.pos_learning_cnt, self.neg_learning_cnt,
self.loss, elapsed_time_epoch))
with self.lock:
logging.info("[{}][Epoch {}/{}] Epsilon:{:.4f} "
"#Expl.:{}/{} #Buy:{} #Sell:{} #Hold:{} "
"#Stocks:{} PV:{:,.0f} "
"POS:{} NEG:{} Loss:{:.6f} ET:{:.4f}".format(
self.stock_code, epoch_str,
num_epoches, epsilon,
self.exploration_cnt, self.itr_cnt,
self.agent.num_buy, self.agent.num_sell,
self.agent.num_hold, self.agent.num_stocks,
self.agent.portfolio_value,
self.pos_learning_cnt, self.neg_learning_cnt,
self.loss, elapsed_time_epoch))

# 에포크 관련 정보 가시화
self.visualize(epoch_str, num_epoches, epsilon)
Expand All @@ -349,10 +353,11 @@ def run(
elapsed_time = time_end - time_start

# 학습 관련 정보 로그 기록
logging.info("[{code}] Elapsed Time:{elapsed_time:.4f} "
"Max PV:{max_pv:,.0f} #Win:{cnt_win}".format(
code=self.stock_code, elapsed_time=elapsed_time,
max_pv=max_portfolio_value, cnt_win=epoch_win_cnt))
with self.lock:
logging.info("[{code}] Elapsed Time:{elapsed_time:.4f} "
"Max PV:{max_pv:,.0f} #Win:{cnt_win}".format(
code=self.stock_code, elapsed_time=elapsed_time,
max_pv=max_portfolio_value, cnt_win=epoch_win_cnt))

def build_sample(self):
self.environment.observe()
Expand Down

0 comments on commit 57c6234

Please sign in to comment.