ltp() in thread caused exception.

Rupali
Got execption when ltp() was in a thread. More than 2000 thread were calling ltp(). Here is error of the code. What is solution ?

return self._get("market.quote.ltp", params={"i": ins})
raise exp(data["message"], code=r.status_code)
File "C:\Users\sai\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 836, in _get
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
kiteconnect.exceptions.NetworkException: Too many requests
File "C:\Users\sai\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 912, in _request
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
File "C:\Users\sai\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 912, in _request
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
File "C:\Users\sai\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 912, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.NetworkException: Too many requests
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.NetworkException: Too many requests
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.NetworkException: Too many requests
Tagged:
  • rakeshr
    kiteconnect.exceptions.NetworkException: Too many requests
    As, the error message says, you are exceeding the rate limits, defined here.
    More than 2000 thread were calling ltp()
    You need to reduce down the number of concurrent threads, with in the rate limit.
  • Rupali
    1. Is there any limit on number of concurrent threads?
    2. Can upgrading h/w solve the issue with concurrent threads?
  • tahseen
    @Rupali

    1. You can run as many threads as you want so long as you comply with rate limit. Read #3
    2. How would upgrading hardware solve ? Putting a super computer at your side still won't permit you to hit millions requests per second. Your hardware would allow but Zerodha server won't accept beyond rate limit
    3. Please read about API rate limit https://kite.trade/forum/discussion/2760/no-of-request-to-api
  • Rupali
    @tahseen @rakeshr


    Then calling kite.quote() in a thread for every symbol is impacting performance after putting a sleep of 1 second. Could you please help me with some other solution?
  • tahseen
    @Rupali

    1. Install Redis or Memcache
    2. Create an independent thread, whose task should be bulk batch polling for LTP of stocks you are looking for and it then updates a cache like Redis
    3. Ensure this #1 thread internally honours the rate limit
    4. Now all other threads or modules that need this LTP read from Redis
  • Rupali
    @tahseen

    My objective is to get LTP of almost 2000 symbols every time during market hours. I think Websocket can solve my problem. Pls confirm.
  • tahseen
    @Rupali Yes, I was wondering why you are fetching via LTP

    You can use web socket and write it to Redis and then any thread can read from there. This is best way

    Note, don't do any computation inside
    on_ticks

    Just read and push it to a queue from there. And then the function that reads from that queue would do writing to Redis
  • Rupali
    Thank you so much @tahseen ..
Sign In or Register to comment.