diversify icon indicating copy to clipboard operation
diversify copied to clipboard

Use aiohttp to speed up HTTP request by making asynchronous calls

Open edujtm opened this issue 7 years ago • 3 comments

Due to pagination, some methods need to make multiple requests to the spotify API in order to get all the songs from big playlists. This is currently made synchronously so it takes a long time to complete. To fix this, the ideia is to make the call asynchronously to multiple endpoints. I tried implementing this before on the tf-clustering branch, but since I did not limit the number of requests, I was getting blocked by the API.

But I imagine that the aiohttp library has support to add a limit to the number of requests it's sending. If it it doesn't the plan b is to use asyncio.BoundedSemaphore.

Current Paginator: (There is a lot of time since I've written this class, the asyncio module might have changed and this code might not work anymore)

class AsyncPaginator:
    def __init__(self, spfy, paging_object):
        """
            This class tries to make the requests for pagination objects
            from the Spotify Web API asynchronoursly, but it still not used
            because of rate limitations from the api
            when making a lot of requests in parallel.
        :param spfy: The Spotipy Session Object
        :param paging_object: A Paging Object from Spotify Web API
        """
        self.paging = paging_object
        self.limit = paging_object['limit']
        self.total = paging_object['total']
        self.executor = futures.ThreadPoolExecutor(10)
        self.spfy = spfy
        self.loop = asyncio.get_event_loop()

    def run(self):
        # The first request is made synchronously to get the base url
        first = [self.paging]

        old_url = urlparse(self.paging['href'])

        base_url = old_url.scheme + '://' + old_url.netloc + old_url.path

        tasks = []
        for i in range(self.limit, self.total, self.limit):
            new_url = base_url + f'?offset={i}&limit={self.limit}'
            task = asyncio.ensure_future(self.get_next(new_url))
            tasks.append(task)

        # Run all requests in the thread pool, making them asynchronous
        results = asyncio.gather(*tasks)

        rest = self.loop.run_until_complete(results)
        first.extend(rest)
        return first

    async def get_next(self, url):
        result = {'next': url}
        return await self.loop.run_in_executor(self.executor, self.spfy.next, result)

edujtm avatar Oct 03 '19 02:10 edujtm

Implemented on asyncutils branch, seems to be working fine, but I didn't test it yet.

edujtm avatar Oct 04 '19 03:10 edujtm

@edujtm Hello, do you still need this ?

kirillkuzin avatar Oct 04 '19 08:10 kirillkuzin

@kirillkuzin I think not. I let the issue still open because I didn't test the implementation that I did, but I think it's probably working correctly. I'll close the issue once I get more time.

edujtm avatar Oct 04 '19 17:10 edujtm