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

실시간데이터 수신과 반복적인 계좌조회가 multi thread 로 수행될때 gRPC 서버 멈춤 현상 #41

Closed
thesoomyung opened this issue Aug 17, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@thesoomyung
Copy link

thesoomyung commented Aug 17, 2021

실시간데이터 수신과 반복적인 계좌조회가 multi thread 로 수행될때 gRPC 서버 멈춤 현상을 리포트 합니다.
재현 하실 수 있게 real2.py 를 게시합니다.

real2.py

from time import sleep
from koapy.backend.kiwoom_open_api_plus.grpc.KiwoomOpenApiPlusServiceClient import KiwoomOpenApiPlusServiceClient 
import threading
import random

wait=5
evt = threading.Event()
def getreal(kiwoom, codes,fids,screenno):
    global evt
    stream = kiwoom.GetRealDataForCodesAsStream(codes,
                                                fids=fids,
                                                screen_no=screenno)
    for event in stream:
        if evt.is_set():
            stream.cancel()
            break

    return

def getaccount(kiwoom):
    global evt
    while True:
        s,m=kiwoom.GetAccountEvaluationStatusAsSeriesAndDataFrame(account_no='800409****')
        print(m)
        sleep(wait)
        if evt.is_set():
            break
    return

def start_account_thread(kiwoom):
    t=threading.Thread(target=getaccount, args=(kiwoom,))
    t.start()
    print(f"Account thread started\n")
    return t

def start_real_thread(kiwoom):
    codes=["252670","005930","122630","302440","111710","293490","323410"]
    fids=[10,11,12,27,28,16,17,18,26,311]
    scrno = str(random.randrange(1000,9999))
    t=threading.Thread(target=getreal, args=(kiwoom,codes,fids,scrno))
    t.start()
    print(f"Realtime thread started")
    return t

def start():
    print("Started")
    kiwoom = KiwoomOpenApiPlusServiceClient()
    kiwoom.EnsureConnected()

    t_account = start_account_thread(kiwoom)
    t_real    = start_real_thread(kiwoom) 
    
    sleep(600)
    print('sleep done')
    evt.set() # 600초 뒤에 모든 Thread에 종료 시그널을 보냅니다.
    t_account.join()
    print(f"Account thread terminated\n")
    t_real.join()
    print(f"Realtime thread terminated\n")
    print("Finished")

if __name__=="__main__":
    start()

real2.py 의 동작은 다음과 같습니다.

  1. "5초마다 계좌를 조회"하는 쓰레드 생성 : start_account_thread()
  2. "7개 종목데이터를 실시간 수신"하는 쓰레드 생성 : start_real_thread()
  3. 600초 뒤에 모든 쓰레드를 종료

테스트방법

터미널을 열어 서버를 띄웁니다.
koapy version: 0.5.1

koapy serve

real2.py 에서 account_no='800409****' 의 계좌번호를 본인의 것으로 수정합니다.
터미널을 한개 더 띄워서 real2.py 를 실행시킵니다.

python real2.py

문제현상

처음 1~2분간은 순조롭게 2개 쓰레드가 정상 수행됩니다.
이후 서버가 갑자기 멈추면서 쓰레드 역시 동작을 멈춥니다.

@thesoomyung thesoomyung added the bug Something isn't working label Aug 17, 2021
elbakramer added a commit that referenced this issue Aug 20, 2021
@elbakramer
Copy link
Owner

0.6.0 버전으로 테스트 부탁드립니다.

@thesoomyung
Copy link
Author

이슈 해결된 것 같습니다.
문제 시나리오 이틀간 테스트해봤는데, 서버 응답없는 현상 나타나지 않았습니다.
감사합니다!

@elbakramer
Copy link
Owner

문제 해결되서 이슈 닫습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants