New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sleep-on-rate-limit not working #350
Comments
What version of python-twitter are you using and/or can you test against the In the interim, I checked out what the problem and it looks like it's Twitter being inconsistent in the rate limit returned in their URL headers, which may be a temporary problem or may be something that we need to take into account. For instance, for the code: for i in range(0, 175):
api.GetSearch(term="primary", count=100)
print(api.rate_limit.get_limit('/search/tweets')) I am essentially getting two sets of rate limits:
Where one (the lower one) is the real rate limit and the higher one is maybe cached on their servers or whatever server is counting their rate limits is being inconsistently load balanced? I'm not sure. What I'd suggest you do in the meantime (since there's nothing we can do about this right now), is something like: for user in users:
results = api.GetSearch(
term="from:"+user,
since_id=sincetweet,
max_id=maxtweet,
count=10,
result_type="recent")
time.sleep(5.05) The time.sleep(5.05) call will ensure that you never go over the rate limit for the GetSearch endpoint (15 minute window (900 seconds) divided by 180 allowed calls + a small amount of padding). |
It's worth noting that Twitter's having issues serving images today as well, so it might be temporary. Either way, you can enforce the correct sleeping behavior manually. |
I downloaded the newest version about a week ago. 3-something, I believe? |
(I only asked since I noticed that the line numbers were different from Yeah, I hope that's a reasonable solution for the moment. It's one of those things where we rely on the information coming back from Twitter, so I'm not really sure how we want to account for inconsistencies in that data, but, generally speaking, if you are having rate limit error issues, even when using the built in I'll check out the code above later this week and see if it has fixed itself (if it seems like it has, I'll close the issue). If you run into any further problems, feel free to reopen the issue or file another one. Thanks! |
Thanks! On Wed, Jun 8, 2016 at 10:02 PM, Jeremy Low notifications@github.com
|
I changed |
This seems to be working properly now with the correct rate limit returned in all cases tested, so I'm going to close the issue as it seems like it was a problem with Twitter's servers. Please reopen if you're still having a problem and maybe we can get to the bottom of it. |
I have a program that iterates through all available tweet search results and it throws api = twitter.Api(
consumer_key='a',
consumer_secret='b',
access_token_key='c',
access_token_secret='d',
sleep_on_rate_limit=True
)
last_id = None
while True:
tweets = api.GetSearch(term='obama', count=100, max_id=last_id)
if len(tweets) == 0:
break
for tweet in tweets:
print(tweet.text)
last_id = tweet.id Shouldn't python-twitter sleep for the correct amount of time so that this exception doesn't happen? |
We do, if you see @jeremylow 's comment earlier we often see that different edge servers in Twitter's fleet will respond with different rate-limit values which cause the code to not slow down, and then the next cycle of requests hit another server that has a lower limit and you get limited. He also mentioned a work-around that allows for a less painful experience |
There may be an issue with rounding. Mind if I just push a small padding value on the sleep call to master, @bear ? |
Why not catch the exception if the user is asking python-twitter to handle rate limiting? I mean, I guess I can too, but it kinda defeats the purpose of asking python-twitter to manage it. |
Ok, so there's a small padding value (t <= 2s) since we were seemingly sleeping for <= 1 too few seconds. @edsu if you could give this a shot, I'd be interested in your results. As for catching the error, personally I'd prefer managing it this way and reducing the number of times our library is responsible for going over Twitter's limits. (See also #358 for why I'd rather not dictate the user's actions on hitting a rate limit. A |
(I mean, the user's has said that they'd like the API to sleep, so yeah, that concern is probably moot.) |
I initialized with sleep_on_rate_limit set to true, like this:
api = twitter.Api(consumer_key = CONSUMER_KEY,
consumer_secret=CONSUMER_SECRET,
access_token_key=ACCESS_TOKEN,
access_token_secret=ACCESS_SECRET,
sleep_on_rate_limit=True)
I have a long list of users I am iterating through, and I'm getting their tweets from the last week using the GetSearch command:
results = api.GetSearch(term="from:"+user,
since_id=sincetweet,
max_id=maxtweet,
count=10,
result_type="recent")
I was expecting my program to sleep while waiting for Twitter's rate limit to refresh, but it instead pauses for a few minutes and then throws a "Rate limit exceeded" code: 88 error, like this:
Traceback (most recent call last):
File ".../collect_user_tweets.py", line 45, in
result_type="recent")
File ".../venv/lib/python3.5/site-packages/twitter/api.py", line 424, in GetSearch
data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))
File ".../venv/lib/python3.5/site-packages/twitter/api.py", line 4688, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File ".../venv/lib/python3.5/site-packages/twitter/api.py", line 4715, in _CheckForTwitterError
raise TwitterError(data['errors'])
twitter.error.TwitterError: [{'message': 'Rate limit exceeded', 'code': 88}]
Any ideas?
The text was updated successfully, but these errors were encountered: