1

I have a batch of mp3 files and I have to get data on each using the python shazamio library. I am using asyncio.gather() to run all the recognise function, however each song is waiting for the previous song to finish first. For example, if I run one song, it is taking 8s to get the data. But if I run three songs, it is taking 23 to 25 seconds to get the data.

Code:

from shazamio import Shazam
import asyncio

shazam = Shazam()

files = [
'./0.mp3',
'./1.mp3',
'./2.mp3',
]

async def recognise_one_song(name):
    print(name)
    song = await shazam.recognize_song(name)
    if song.get('track') != None:
        print(song['track']['title'] + '\t' + song['track']['subtitle'])

async def recognise_all_songs(files):
    tasks = []
    for file in files:
        tasks.append(recognise_one_song(file))
    await asyncio.gather(*tasks)

asyncio.run(recognise_all_songs(files))

The output looks like this, so perhaps functions start executing concurrently but API calls are not:

./0.mp3
./1.mp3
./2.mp3
Nisargadatta (Original Mix)     Morten Granau
Nisargadatta (Original Mix)     Morten Granau
Nisargadatta (Original Mix)     Morten Granau
  • That output looks correct. How do you know the API calls aren’t running concurrently? – dirn Jun 26 '22 at 17:59

0 Answers0