Rate Limiting API Requests with asyncio, aiohttp and semaphores

Поделиться
HTML-код
  • Опубликовано: 14 ноя 2024

Комментарии • 16

  • @marx427
    @marx427 3 года назад +3

    Thank you so much ! I literally spent so much time putting time.sleep(x) everywhere between my calls but it really didn't helped me. indeed, using semaphores fixed my problem ! I needed to use some games api to retrieve matches from more than 250 players (20 matches approx, by player) and to write them inside a csv :]

  • @GelsYT
    @GelsYT 2 года назад

    THANK YOU! WHAT AN ABSOLUTE LEGEND DESERVE A MILLION SUBS!

  • @DaveThomson
    @DaveThomson 2 года назад

    Update. Httpx has a asyncclient

  • @harrythuku628
    @harrythuku628 4 года назад +4

    could i get a souce code to this?

  • @fahadusman3538
    @fahadusman3538 4 года назад +3

    Could we have the code please. thanks for the vid

  • @karthikv1906
    @karthikv1906 8 месяцев назад

    This works in Python env but not in databricks

  • @SifBaksh
    @SifBaksh 4 года назад +1

    Do you have a link to the code @Ethan Lyon ?

  • @Axell0003
    @Axell0003 Год назад +1

    # Import the libraries needed to run the code.
    import asyncio
    import aiohttp
    import attr
    @attr.s
    class Fetch:

    limit = attr.ib()
    rate = attr.ib(default=5, converter=int)

    async def make_request(self, url, limit):
    async with self.limit:
    async with aiohttp.ClientSession() as session:
    async with session.request(method="GET", url=url) as response:
    json = await response.json()
    status = response.status
    print(f"Made request: {url}, Status: {status}")

    await asyncio.sleep(self.rate)
    async def main(urls, rate, limit):
    limit = asyncio.Semaphore(limit)
    f = Fetch(rate=rate, limit=limit)

    tasks = []

    for url in urls:
    tasks.append(f.make_request(url=url, limit=limit))

    results = await asyncio.gather(*tasks)


    def test_run():
    URL_LIMIT = 20

    urls = [f"httpbin.org/anything/{n}" for n in range(URL_LIMIT)]

    LIMIT = 6
    RATE = 1

    asyncio.run(main(urls=urls, rate=RATE, limit=LIMIT))
    test_run()

  • @harrythuku628
    @harrythuku628 4 года назад +2

    also another question, i tried creating an async function in flask to handle api requests, but it didnt work on flask, but worked as an independent package, help please if you can

  • @Sousleek
    @Sousleek 3 года назад

    please share a github page with the source-code.

  • @mithunnambiar1433
    @mithunnambiar1433 3 года назад

    2 req every call? Where have you mentioned 2 in the code, i don't see it at all

    • @GelsYT
      @GelsYT 2 года назад

      i think it's the semaphore which is called as the limit and the asyncio.sleep that is 2 seconds

    • @georgealexb3940
      @georgealexb3940 Год назад

      He's passing the values from the pytest script that he has written on the left.