Skip to content

fixed #58: session 추가 #59

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

Merged
merged 2 commits into from
Nov 25, 2024
Merged

fixed #58: session 추가 #59

merged 2 commits into from
Nov 25, 2024

Conversation

tasoo-oos
Copy link
Contributor

🛠️ PR Summary

🌟 요약

어떤 것이 변경되었나요? 간략히 설명해주세요.

API 서버에 request를 보낼 때 먼저 세션에 연결합니다.

📊 주요 변경 사항

주요 변경 사항을 적어주세요.

  • PyKis 클래스의 각 인스턴스는 이제 실전/모의투자용 세션 두 개씩을 가지고, 할당 해제될 때 세션도 같이 사라집니다.

🎯 목적 및 영향

  • 목적: 왜 이 PR이 필요한가요?

    • 의미없는 소켓 점유를 줄이고, 세션을 요청별로 열지 않고 재사용하므로 네트워크 리소스를 덜 사용합니다.
  • 영향: 이 변경 사항이 어떤 영향을 미치나요?

    • 불필요한 연결 수립/해제 감소
    • 연결 풀링을 통한 지연 시간 감소
    • 메모리 사용량 최적화
    • TCP 소켓 관리 효율화

테스트

import sys
sys.path.append('/home/tasoo1118/projects/python_projects/python-kis')  # kis 패키지의 경로를 추가합니다.

import time
import psutil
import os

from pykis import PyKis
from dotenv import dotenv_values
from pykrx import stock
import logging

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

config = dotenv_values(".env")  # 개인정보를 저장한 .env 파일을 읽어옵니다.

kis = PyKis(
    id=config["KRX_HTS_ID"],
    account=config["KRX_ACCOUNT_NUMBER"],
    appkey=config["KRX_APP_KEY"],
    secretkey=config["KRX_APP_SECRET"],
    keep_token=True
)

current_process = psutil.Process(os.getpid())

tickers = (
        stock.get_market_ticker_list(market="KOSPI") +
        stock.get_market_ticker_list(market="KOSDAQ") +
        stock.get_etf_ticker_list()
)  # 코스피, 코스닥, ETF 종목을 모두 가져옵니다. (총 3,000여개)

for i, ticker in enumerate(tickers):
    asd = kis.stock(symbol=ticker, market="KRX")
    quote = asd.quote()
    print(f"{i}: {quote.name}")

    print(f"Open sockets: {len(current_process.net_connections())}")
    print()
    time.sleep(1)
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 506
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 506
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 506
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 24406
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): data.krx.co.kr:80
DEBUG:urllib3.connectionpool:http://data.krx.co.kr:80 "POST /comm/bldAttendant/getJsonData.cmd HTTP/11" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): openapi.koreainvestment.com:9443
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=095570 HTTP/11" 200 1885
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/search-info?PDNO=095570&PRDT_TYPE_CD=300 HTTP/11" 200 703
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=095570 HTTP/11" 200 1885
0: AJ네트웍스
Open sockets: 1

DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=006840 HTTP/11" 200 1937
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/search-info?PDNO=006840&PRDT_TYPE_CD=300 HTTP/11" 200 680
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=006840 HTTP/11" 200 1937
1: AK홀딩스
Open sockets: 1

DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=027410 HTTP/11" 200 1880
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/search-info?PDNO=027410&PRDT_TYPE_CD=300 HTTP/11" 200 628
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=027410 HTTP/11" 200 1880
2: BGF
Open sockets: 1

DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=282330 HTTP/11" 200 1931
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/search-info?PDNO=282330&PRDT_TYPE_CD=300 HTTP/11" 200 676
DEBUG:urllib3.connectionpool:https://openapi.koreainvestment.com:9443 "GET /uapi/domestic-stock/v1/quotations/inquire-price?FID_COND_MRKT_DIV_CODE=J&FID_INPUT_ISCD=282330 HTTP/11" 200 1931
3: BGF리테일
Open sockets: 1

...

@Soju06 Soju06 self-assigned this Nov 25, 2024
@Soju06 Soju06 added the 버그 뭔가 작동하지 않습니다. label Nov 25, 2024
@Soju06 Soju06 merged commit 94a903d into Soju06:main Nov 25, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
버그 뭔가 작동하지 않습니다.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants