Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: SetRealReg must be called asynchronously. #78

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import queue
import threading
from time import sleep

import grpc

@@ -167,13 +168,39 @@ 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)
)


def try_to_register(retry=2, timeout=3.0):
retry_count = 0

def call():
KiwoomOpenApiPlusError.try_or_raise(
self.control.SetRealReg.async_call(screen_no, code, fid_list_joined, opt_type),
except_callback=on_error
)

def on_error(e):
nonlocal retry_count
if isinstance(e, KiwoomOpenApiPlusError):
error_message = f"Failed to register {code=}. Reason: {e} ({e.code})."
else:
error_message = f"Failed to register {code=}. Reason: {e}."
if retry_count < retry:
retry_count += 1
self.logger.warning(f"{error_message} Retrying ({retry_count}/{retry}) in {timeout} ...")
else:
self.logger.warning(f"{error_message} Kiwoom server does not allow register.")
return
sleep(timeout)
call()

call()

try_to_register()

def remove_code(self, code):
if code in self._screen_by_code:
screen_no = self._screen_by_code[code]