'How to speed up Spotify API calls?
I am retrieving track data from spotify api for a total of 10 tracks, but it takes around 2-3 seconds to run. Is there any way to speed it up by using some python libraries like multiprocessing or something else.
track_url = []
track_name = []
album_image = []
for i in range(len(tracks_recommend)):
track_id = tracks_recommend.at[i, 'id']
# call to spotify api
res = spotify.track(track_id=track_id)
track_url.append(res['external_urls'])
track_name.append(res['name'])
album_image.append(res['album']['images'][0]['url'])
Solution 1:[1]
Depends on whether Spotify tracks you and limits you to one request. If they don't, this would be what you could start with:
def process_track(track_id)
# call to spotify api
res = spotify.track(track_id=track_id)
return (res['external_urls'], res['name'], res['album']['images'][0]['url'])
with Pool(4) as p: # replace 4 with whatever number you want
track_ids = [tracks_recommend.at[i, 'id'] for i in range(len(tracks_recommend))]
output = p.map(process_track, track_ids)
track_url, track_name, album_image = zip(*output)
This won't help you with latency, but it might increase throughput.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zorgoth |
