Skip to content

Commit

Permalink
Trying solution: executing SetRealReg breaking channel.
Browse files Browse the repository at this point in the history
  • Loading branch information
dh377 committed Apr 22, 2022
1 parent 3c93e34 commit df2911b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import queue
import threading
from time import sleep

import grpc

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit df2911b

Please sign in to comment.