0

I'm using python github3 module and i need to set delay between request to github api, because my app make to much load on server.

I'm doing things such as

git = github3.GitHub()
for i in itertools.chain(git.all_repositories(), git.repositories(type='private')):
    do things

I found that GitHub use requests to make request to github api. https://github.com/sigmavirus24/github3.py/blob/3e251f2a066df3c8da7ce0b56d24befcf5eb2d4b/github3/models.py#L233

But i can't figure out what parameter i should pass or what atribute i should change to set some delay between the requests.

Can you advise me something?

1
  • Could you clarify what you mean by "my app make too much load on server"? Are you hitting a request limit imposed by the GitHub API? If so, why not catch the corresponding HTTP response and implement a back off? Commented May 18, 2017 at 18:34

2 Answers 2

0

github3.py presently has no options to enforce delays between requests. That said, there is a way to get the request metadata which includes the number of requests you have left in your ratelimit as well as when that ratelimit should reset. I suggest you use git.rate_limit()['resources']['core'] to determine what delays you should set for yourself inside your own loop.

Sign up to request clarification or add additional context in comments.

Comments

0

I use the following function when I expect to exceed my query limit:

def wait_for_karma(gh, min_karma=25, msg=None):
    while gh:
        core = gh.rate_limit()['resources']['core']
        if core['remaining'] < min_karma:
            now = time.time()
            nap = max(core['reset'] - now, 0.1)
            logger.info("napping for %s seconds", nap)
            if msg:
                logger.info(msg)
            time.sleep(nap)
        else:
            break

I'll call it before making a call that I believe is "big" (i.e. could require multiple API calls to satisfy). Based on your code sample, you may want to do this at the bottom of your loop:

git = github3.GitHub()
for i in itertools.chain(git.all_repositories(), git.repositories(type='private')):
    do_things()
    wait_for_karma(git, msg="pausing")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.