Giter Club home page Giter Club logo

Comments (15)

SharpBit avatar SharpBit commented on May 30, 2024

Hey, I'm currently away right now, I'll look into it more when I get back. Thanks for using brawlstats!

from brawlstats.

burneyy avatar burneyy commented on May 30, 2024

Hi,
so I was also looking into this and found two possible solutions:

  1. asyncio.Lock() (preferred)
    Initialize an self.lock = asyncio.Lock() in the client and modify _aget_model to
async def _aget_model(self, url, model, key=None):
  async with self.lock:
    data, resp = await self._arequest(url)
    await asyncio.sleep(1./self.ratelimit)
...

Like this, one can call the line of code above

players = await asyncio.gather(*(client.get_player(player.tag) for player in club.members))

as intended.

  1. Provide another method intended for sending multiple requests, e.g. (however could be easily archived as well by using method 1)
async def _aget_models(self, urls, model, key=None):
  tasks = []
  for url in urls:
    tasks.append(asyncio.ensure_future(self._aget_model(url, model=model, key=key)))
    await asyncio.sleep(1./self.ratelimit)

  results = await asyncio.gather(*tasks)
  return results

Please let me know what you think and enjoy your holidays!

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

I'm assuming if I implement option 1, then users who are only requesting for 1 url will get their results 1/3 seconds slower (as the user rate limit is 3 reqs/sec). However, it would be much simpler than the second option.

from brawlstats.

burneyy avatar burneyy commented on May 30, 2024

Possibly yes but I guess the answer anyway takes 1/3 seconds before it arrives. And I guess one could also argue that there is no need to use it asynchronously when you only request one url. However, maybe one could also make it an option to specify a ratelimit when initialising the client (and set it to 0 if one does not want it)...

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

the discord.py wrapper is asynchronous and it is best practice to use async libraries for a discord bot. I could make an option when initializing the client such as prevent_ratelimit=True if you want to use the asyncio.Lock()

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

@burneyy added in ccbca58, should be out in v2.3.6

from brawlstats.

burneyy avatar burneyy commented on May 30, 2024

@SharpBit Very good! What do you think, should there also be an automatic retry after X seconds when there was a rate limit error still? It seems to still occur occasionally. Like they do here: https://github.com/Rapptz/discord.py/blob/master/discord/http.py#L182

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

I could try but that is currently not a high priority and I have some other stuff to do in the next week or two

from brawlstats.

thecaralice avatar thecaralice commented on May 30, 2024

I'm assuming if I implement option 1, then users who are only requesting for 1 url will get their results 1/3 seconds slower (as the user rate limit is 3 reqs/sec). However, it would be much simpler than the second option.

Good practice is to send ctx.typing in these cases

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

@MinerChAi I don't know if he is making a discord bot or some other application

from brawlstats.

burneyy avatar burneyy commented on May 30, 2024

@MinerChAI I don't know if he is making a discord bot or some other application

Nope I am not making a discord bot so I also don't know what ctx.typing is I must say..

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

Hey, just wanted to leave a comment that if you upgraded to v2.3.6, the wrapper probably was broken. This is due to the new API server (api.brawlapi.cf/v1) having timeout issues, if you have v2.3.6, please upgrade to v2.3.7 @burneyy @MinerChAI. Sorry for the inconvenience and the API developers are working at it.

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

You are right, that is odd (burneyy). I am still getting occasional RateLimitErrors that are failing the tests. When I test locally, I do not get ratelimited but it seems on travis-ci that I am. I wonder if it is hitting the global ratelimit or if it's the user ratelimit.

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

@burneyy Update to v2.3.8 Rate limits do happen, but it is a bit less often. I am trying to work towards no rate limits at all. However, please still update as the old Base URL will be deprecated tomorrow or in the next week and v2.3.7 and below will not work unless you specify the new base url when creating the client

from brawlstats.

SharpBit avatar SharpBit commented on May 30, 2024

Was requested to be closed.

from brawlstats.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.