小编123*_*134的帖子

如何并行化python api调用?

我正在开发一个程序,当我的艺术家在 Spotify 上有新音乐时,它会通过电子邮件发送给我。它通过在脚本运行时获取每个艺术家拥有的专辑数量并将结果与​​保存为 CSV 文件的前一天进行比较来实现这一点。

这涉及 API 调用以验证艺术家是否在 Spotify 上(我收到某些专辑不在 Spotify 上的错误),然后获取该艺术家的专辑数量。这些电话非常耗时,尤其是当我有近千名艺术家时。

我想知道如何并行化这些 API 调用或任何其他建议以加速整个程序。下面链接的是具有 API 调用的代码部分。提前感谢您的时间。

# given artist name returns all info related to artist 
def get_artist_info(spotipy_instance, name):
    results = spotipy_instance.search(q='artist:' + name, type='artist')
    items = results['artists']['items']
    if len(items) > 0:
        return items[0]
    else:
        return None

# returns list of all albums given artist name 
def get_artist_albums(spotipy_instance, artist):
    albums = []
    results = spotipy_instance.artist_albums(artist['id'], album_type='album')
    albums.extend(results['items'])
    while results['next']:
        results = spotipy_instance.next(results)
        albums.extend(results['items'])
    seen = set() # to avoid …
Run Code Online (Sandbox Code Playgroud)

python parallel-processing spotify python-requests

5
推荐指数
1
解决办法
5881
查看次数