[GH-ISSUE #956] Spotipy's sleep_for_retry() function? #576

Closed
opened 2026-02-27 23:23:26 +03:00 by kerem · 1 comment
Owner

Originally created by @CalColistra on GitHub (Mar 30, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/956

I have been using spotify's spotipy (python) api and my program is getting stuck on a sleep_for_retry(). I can see the sequence of functions being called in my python notebook and it gets stuck at this:
artist() > _get() >_internal_call() > request() > send() > send() > urlopen() > sleep() > sleep_for_retry()

Wondering how to avoid their sleep_for_retry() function? I am struggling to find any documentation regarding that function but i have seen some people suggesting this: time.sleep(<some amount of time here>) but I can't figure out how to get that working.

Does anyone have experience with this? I love the spotipy API and I would like to keep using it but I don't want to be making more requests than their server can handle.

the error looks like this:

--> 321         return self._internal_call("GET", url, payload, kwargs)
    322 
    323     def _post(self, url, args=None, payload=None, **kwargs):

/usr/local/lib/python3.9/dist-packages/spotipy/client.py in _internal_call(self, method, url, payload, params)
    262 
    263         try:
--> 264             response = self._session.request(
    265                 method, url, headers=headers, proxies=self.proxies,
    266                 timeout=self.requests_timeout, **args

/usr/local/lib/python3.9/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    527         }
    528         send_kwargs.update(settings)
--> 529         resp = self.send(prep, **send_kwargs)
    530 
    531         return resp

/usr/local/lib/python3.9/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    643 
    644         # Send the request
--> 645         r = adapter.send(request, **kwargs)
    646 
    647         # Total elapsed time of the request (approximately)

/usr/local/lib/python3.9/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438         try:
    439             if not chunked:
--> 440                 resp = conn.urlopen(
    441                     method=request.method,
    442                     url=url,

/usr/local/lib/python3.9/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    874 
    875             response.drain_conn()
--> 876             retries.sleep(response)
    877             log.debug("Retry: %s", url)
    878             return self.urlopen(

/usr/local/lib/python3.9/dist-packages/urllib3/util/retry.py in sleep(self, response)
    426 
    427         if self.respect_retry_after_header and response:
--> 428             slept = self.sleep_for_retry(response)
    429             if slept:
    430                 return

/usr/local/lib/python3.9/dist-packages/urllib3/util/retry.py in sleep_for_retry(self, response)
    405         retry_after = self.get_retry_after(response)
    406         if retry_after:
--> 407             time.sleep(retry_after)
    408             return True
Originally created by @CalColistra on GitHub (Mar 30, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/956 I have been using spotify's spotipy (python) api and my program is getting stuck on a sleep_for_retry(). I can see the sequence of functions being called in my python notebook and it gets stuck at this: `artist() > _get() >_internal_call() > request() > send() > send() > urlopen() > sleep() > sleep_for_retry()` Wondering how to avoid their sleep_for_retry() function? I am struggling to find any documentation regarding that function but i have seen some people suggesting this: `time.sleep(<some amount of time here>)` but I can't figure out how to get that working. Does anyone have experience with this? I love the spotipy API and I would like to keep using it but I don't want to be making more requests than their server can handle. the error looks like this: ``` --> 321 return self._internal_call("GET", url, payload, kwargs) 322 323 def _post(self, url, args=None, payload=None, **kwargs): /usr/local/lib/python3.9/dist-packages/spotipy/client.py in _internal_call(self, method, url, payload, params) 262 263 try: --> 264 response = self._session.request( 265 method, url, headers=headers, proxies=self.proxies, 266 timeout=self.requests_timeout, **args /usr/local/lib/python3.9/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 527 } 528 send_kwargs.update(settings) --> 529 resp = self.send(prep, **send_kwargs) 530 531 return resp /usr/local/lib/python3.9/dist-packages/requests/sessions.py in send(self, request, **kwargs) 643 644 # Send the request --> 645 r = adapter.send(request, **kwargs) 646 647 # Total elapsed time of the request (approximately) /usr/local/lib/python3.9/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies) 438 try: 439 if not chunked: --> 440 resp = conn.urlopen( 441 method=request.method, 442 url=url, /usr/local/lib/python3.9/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw) 874 875 response.drain_conn() --> 876 retries.sleep(response) 877 log.debug("Retry: %s", url) 878 return self.urlopen( /usr/local/lib/python3.9/dist-packages/urllib3/util/retry.py in sleep(self, response) 426 427 if self.respect_retry_after_header and response: --> 428 slept = self.sleep_for_retry(response) 429 if slept: 430 return /usr/local/lib/python3.9/dist-packages/urllib3/util/retry.py in sleep_for_retry(self, response) 405 retry_after = self.get_retry_after(response) 406 if retry_after: --> 407 time.sleep(retry_after) 408 return True ```
kerem 2026-02-27 23:23:26 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@stephanebruckert commented on GitHub (Mar 30, 2023):

spotipy should already be waiting/sleeping when the Spotify API response requests to wait (retry-after value). Maybe there is a scenario where this does not apply.

Therefore I'm asking, would you have a Minimal reproducible example that would allow us to reproduce this rate limiting? This would need to work with a new Spotify app.

Feel free to include logs about the current retry-after value, maybe with inspiration from https://github.com/spotipy-dev/spotipy/issues/913#issuecomment-1474314058?

I'm closing to close so we can all discuss in the same thread. Please post your response in https://github.com/spotipy-dev/spotipy/issues/913

<!-- gh-comment-id:1491016556 --> @stephanebruckert commented on GitHub (Mar 30, 2023): spotipy should already be waiting/sleeping when the Spotify API response requests to wait (`retry-after` value). Maybe there is a scenario where this does not apply. Therefore I'm asking, would you have a [Minimal reproducible example](https://en.wikipedia.org/wiki/Minimal_reproducible_example) that would allow us to reproduce this rate limiting? This would need to work with a new Spotify app. Feel free to include logs about the current retry-after value, maybe with inspiration from https://github.com/spotipy-dev/spotipy/issues/913#issuecomment-1474314058? I'm closing to close so we can all discuss in the same thread. Please post your response in https://github.com/spotipy-dev/spotipy/issues/913
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#576
No description provided.