KiteTicker consuming 100% cpu

wishabhilash
Hi I am using Python kiteconnect==3.7.0b9.
When using KiteTicker, I am noticing 100% cpu usage on an average.

I tried in my machine and also in a remote aws machine (m3 medium) and in both of them the cpu spikes to 100% usage.

However, when I tried the same with KiteConnect node (kiteconnect@beta) API, CPU usage is nominal.

What can the issue be?
  • sujith
    Can you paste your code?
  • wishabhilash
    Here you go:

    from kiteconnect import KiteConnect, KiteTicker
    import json
    from datetime import datetime
    import time
    from src.settings import Config
    from src.feed import tasks
    from src.common.pubsub import Producer
    import logging
    from src.common import utils

    config = Config()

    class Ticker(KiteTicker):
    api_key = config.stock_broker['API_KEY']

    def __init__(self, args):
    self.args = args
    access_token = self.get_connected()
    super().__init__(self.api_key, access_token, reconnect_max_tries=3, reconnect_max_delay=5)
    self.assign_callbacks()

    def get_connected(self):
    self.kite = KiteConnect(api_key=self.api_key)
    print(self.kite.login_url())
    self.session = self.kite.generate_session(
    config.stock_broker['REQUEST_TOKEN'],
    api_secret=config.stock_broker['SECRET']
    )
    access_token = self.session["access_token"]
    self.kite.set_access_token(access_token)
    return access_token

    def assign_callbacks(self):
    self.on_ticks = self.__on_ticks
    self.on_connect = self.__on_connect
    self.on_close = self.__on_close
    self.on_error = self.__on_error
    self.on_noreconnect = self.__on_noreconnect

    self.tokens = [self.args['instrument']]

    def __on_ticks(self, ws, tick):
    quote = {
    'timestamp': tick[0]['timestamp'].timestamp() * 1000,
    'quote': tick
    }
    tasks.process_quotes.delay(quote, True)
    print(quote)

    def __on_connect(self, ws, response):
    print("on connect: {}".format(response), self.tokens)

    def __on_close(self, ws, code, reason):
    logging.error("closed connection on close: {} {}".format(code, reason))

    def __on_noreconnect(self, ws):
    utils.post_to_slack_channel("Ticker disconnected. Please connect again. :( %s" % self.kite.login_url())
    print("Informed slack. %s" % self.kite.login_url())
    # new_tokens = self.kite.renew_access_token(
    # self.session['refresh_token'], config.stock_broker['SECRET']
    # )
    # self.session.update(new_tokens)
    # self.socket_url = self.get_socket_url(self.api_key, self.session['access_token'])
    # config.stock_broker['REQUEST_TOKEN'] = self.session['request_token']
    # self._create_connection(self.socket_url)


    def __on_error(self, ws, code, reason):
    # utils.post_to_slack_channel("Ticker error. %s %s. :(" % (code, reason))
    print("Informed slack. %s" % self.kite.login_url())

    def get_socket_url(self, api_key, access_token):
    return "{root}?api_key={api_key}"\
    "&access_token={access_token}".format(
    root=self.root,
    api_key=api_key,
    access_token=access_token
    )
  • satish_p
    @Admin, may I know why is my post not approved ?. Its been 4 days my query is still in draft.

    I am unable to connect for KiteTicker

    Getting below error:

    DEBUG:kiteconnect.ticker:Start WebSocket connection.
    2019-02-28 23:37:21+0530 [-] Log opened.
    2019-02-28 23:37:21+0530 [-] failing WebSocket opening handshake ('WebSocket connection upgrade failed (403 - Forbidden)')
    2019-02-28 23:37:21+0530 [-] dropping connection to peer tcp4:13.126.62.119:443 with abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2019-02-28 23:37:21+0530 [-] error...
    DEBUG:root:Ticks: connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    2019-02-28 23:37:21+0530 [-] close...
    2019-02-28 23:37:21+0530 [-] will retry in 2 seconds
    2019-02-28 23:37:21+0530 [-] Stopping factory
    2019-02-28 23:37:21+0530 [-] Main loop terminated
  • sujith
    @satish_p,
    You seem to be using an invalid access token. A 403 means your session is expired and you need to generate a new access token.
  • satish_p
    satish_p edited March 2019
    @sujith
    When I use the same on KiteConnect, I can place orders.
    Basically I tried 3 things.
    1. Generate a token and use it with KiteConnect and KiteTicker (with a gap of less than 2 sec)
    2. Generate a token use it with only KiteConnect
    3. Generate a token use it with only KiteTicker

    KiteConnect works always but KiteTicker never worked till now. I tried the example from Documents, also the threaded solutions and various other solutions mentioned on dev forum.
    Also I have now set the postback url as https.

    Please suggest.
  • satish_p
    @sujith
    I am able to connect now. Was using request_token instead of access_token.

    BTW, Although I dont need my query to be posted now, I want to know why it is still in drafts ? Did I post it the correct way ?
    (Will be helpful for future queries)
  • tahseen
    @wishabhilash You are using
    tasks.process_quotes.delay(quote, True)

    You have not posted what is in this
    process_quotes

    this function processing quotes might have been written incorrectly or might not be optimized
Sign In or Register to comment.