From df2911bc89a3f9288bd1f15ee89011551422e5f6 Mon Sep 17 00:00:00 2001 From: Dongyoon Han <5795942+dh377@users.noreply.github.com> Date: Sat, 23 Apr 2022 06:07:29 +0900 Subject: [PATCH] Trying solution: executing SetRealReg breaking channel. --- .../KiwoomOpenApiPlusLoginEventHandler.py | 4 +- .../KiwoomOpenApiPlusRealEventHandler.py | 43 ++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusLoginEventHandler.py b/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusLoginEventHandler.py index 2a87d29f..6237497e 100644 --- a/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusLoginEventHandler.py +++ b/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusLoginEventHandler.py @@ -26,10 +26,10 @@ def __init__(self, control, request, context): def on_enter(self): if self._credentials is not None: self.control.DisableAutoLogin() - KiwoomOpenApiPlusError.try_or_raise(self.control.CommConnect(), except_callback=self.observer.on_error) + KiwoomOpenApiPlusError.try_or_raise(self.control.CommConnect()) self.control.LoginUsingPywinauto(self._credentials) else: - KiwoomOpenApiPlusError.try_or_raise(self.control.CommConnect(), except_callback=self.observer.on_error) + KiwoomOpenApiPlusError.try_or_raise(self.control.CommConnect()) def OnEventConnect(self, errcode): if errcode < 0: diff --git a/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusRealEventHandler.py b/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusRealEventHandler.py index 6de4cd28..0a3253b4 100644 --- a/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusRealEventHandler.py +++ b/koapy/backend/kiwoom_open_api_plus/grpc/event/KiwoomOpenApiPlusRealEventHandler.py @@ -1,5 +1,6 @@ import queue import threading +from time import sleep import grpc @@ -72,8 +73,7 @@ def on_enter(self): code_list_joined, self._fid_list_joined, self._opt_type_final, - ), - except_callback=self.observer.on_error + ) ) def OnReceiveRealData(self, code, realtype, realdata): @@ -168,14 +168,37 @@ def register_code(self, code, fid_list=None): fid_list_joined = ";".join(str(fid) for fid in fid_list) else: fid_list_joined = self._fid_list_joined - self.logger.debug( - "Registering code %s to screen %s with type %s", code, screen_no, opt_type - ) - KiwoomOpenApiPlusError.try_or_raise( - self.control.SetRealReg(screen_no, code, fid_list_joined, opt_type), - except_callback=self.observer.on_error - ) - + + def trying_to_register(retry=3, timeout=3.0): + """ + Sometimes `SetRealReg` is failed some reason from Kiwoom server. + Raised exception in previous implementation breaks gRpc channel immediately. + + Args: + retry (int, optional): _description_. Defaults to 3. + timeout (float, optional): _description_. Defaults to 3.0. + """ + self.logger.debug( + "Registering code %s to screen %s with type %s", code, screen_no, opt_type + ) + trial = 1 + while trial <= retry: + try: + KiwoomOpenApiPlusError.try_or_raise( + self.control.SetRealReg(screen_no, code, fid_list_joined, opt_type) + ) + except KiwoomOpenApiPlusError as e: + self.logger.warning(f"Failed to register {code=} following reason: {e} ({e.code}). Retry ({trial}/{retry}) in {timeout} ...") + except Exception as e: + self.logger.warning(f"Failed to register {code=} following reason: {e}. Retry ({trial}/{retry}) in {timeout} ...") + else: + return + trial += 1 + sleep(timeout) + self.logger.warning(f"{self}: Kiwoom server does not allow register {code=}") + + trying_to_register() + def remove_code(self, code): if code in self._screen_by_code: screen_no = self._screen_by_code[code]