Giter VIP home page Giter VIP logo

Comments (1)

Soju06 avatar Soju06 commented on June 8, 2024

전체 코드를 알 수 없어 정확하게 답변드리기는 어렵지만, 아마 메인 스레드가 종료되어 생긴 문제라고 생각합니다.
위 코드를 이용한 간단한 예제를 참고하시기를 바랍니다.

from pykis import *

with open("B:\\vack.txt", "r") as f:
    APPKEY = f.readline().strip()
    APPSECRET = f.readline().strip()


class TradeLive(object):
    def __init__(self):
        self.kis = PyKis(
            appkey=APPKEY,
            appsecret=APPSECRET,
            virtual_account=True,
        )

    def __enter__(self):
        return self

    def __exit__(self, type_unused, value_unused, traceback_unused):
        pass

    def run(self):
        self.kis.rtclient.event.cntg.add(self.on_cntg)
        self.kis.rtclient.event.oder.add(self.on_oder)
        stock = self.kis.stock("000660")
        stock.rt_add("체결가")

    def on_cntg(self, cli: KisRTClient, res: KisRTPrice):
        print(f'시간: {res.stck_cntg_hour.strftime("%H:%M:%S")}', end=" ")

    def on_oder(self, cli: KisRTClient, res: KisRTConclude):
        print(
            f"{res.acnt_no} {res.acpt_yn_name} {res.cntg_isnm} {res.cntg_yn_name} {res.order_kind_name}",
            end=" ",
        )


app = TradeLive()

app.run()
# PyKis 실시간 조회는 비동기 구조로 작동하므로
# Main thread가 종료되지 않도록 적절한 조치가 필요합니다.

# 1. input 함수를 이용하여 종료를 방지합니다.
input()

# 2. django나 fastapi같은 메인 애플리케이션을 실행합니다.
# uvicorn.run(
#     api,
#     host='0.0.0.0'
#     # ...
# )

# 3. 직접적으로 pykis rtclient의 스레드를 대기합니다.
# app.kis.rtclient.thread.join()

# 4. rtclient가 종료될 때 까지 loop합니다.
# while app.kis.rtclient._close:
#     sleep(1)

위 코드처럼 PyKis 실시간 조회는 비동기 구조로 작동하므로 메인 스레드가 종료되지 않도록 적절한 조치가 필요합니다.
사용하시는 메인 애플리케이션(django, PyQt 등)의 시작 함수를 호출하거나 input 함수로 메인 스레드를 핸들링 해야합니다.

만약 run함수에 대기를 추가하고 싶다면 위 예제의 1~4번 코드를 참고하여 작성하세요.

from python-kis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.