调节/速率限制红宝石机械化
我需要调节 Mechanize 实例与 API 连接的频率(每 2 秒一次,因此限制连接到该或更多)
所以这样:
instance.pre_connect_hooks << Proc.new { sleep 2 }
我原以为这会起作用,而且确实如此,但现在该类中的每个方法都在休眠持续 2 秒,就好像机械化实例被触摸并被告知保持 2 秒一样。我将尝试一个后连接挂钩,但显然我需要一些更复杂的东西,但目前我不知道是什么。
代码是更多解释,因此如果您有兴趣,请继续:https://github.com/blueblank/reddit_modbot ,否则我的问题涉及如何高效且有效地将 Mechanize 实例限制在 API 指定的特定时间范围内(超出该限制会导致请求丢失和禁止)。另外,我猜我也需要更好地将机械化实例集成到我的类中,对此的任何指示也将受到赞赏。
I need to regulate how often a Mechanize instance connects with an API (once every 2 seconds, so limit connections to that or more)
So this:
instance.pre_connect_hooks << Proc.new { sleep 2 }
I had thought this would work, and it sort of does BUT now every method in that class sleeps for 2 seconds, as if the mechanize instance is touched and told to hold 2 seconds. I'm going to try a post connect hook, but it is obvious I need something a bit more elaborate, but what I don't know what at this point.
Code is more explanation so if you are interested following along: https://github.com/blueblank/reddit_modbot, otherwise my question concerns how to efficiently and effectively rate limit a Mechanize instance to within a specific time frame specified by an API (where overstepping that limit results in dropped requests and bans). Also, I'm guessing I need to better integrate a mechanize instance to my class as well, any pointers on that appreciated as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次连接时都会调用前连接钩子和后连接钩子,因此如果存在某种重定向,则可能会为一个请求触发多次。尝试history_added,它只被调用一次:
Pre and post connect hooks are called on every connect, so if there is some redirection it could trigger many times for one request. Try history_added which only gets called once:
我使用 SlowWeb 来限制对特定 URL 的调用。
在这种情况下,对 example.com 域的调用限制为每 60 秒 10 个请求。
I use SlowWeb to rate limit calls to a specific URL.
In this case calls to example.com domain are limited to 10 requests every 60 seconds.