3

If I am not mistaken, you can't speed up individual HTTP requests, but there are ways to speed up multiple requests through asynchronous functions and caching.

Below is an example of a function that prints all the release assets in a GitHub repository.

from github import Github

def get_release_assets(access_token, repository):
    github = Github(access_token)
    repo = github.get_repo(repository)

    releases = list(repo.get_releases())

    for release in releases:
        assets = list(release.get_assets())
        print(assets)

This function takes around 9 seconds to finish in a repository with a relatively small amount of releases.

I am aware that you can use asynchronous functions to speed this up. However, I know that this repository wont be changing constantly, so I am thinking of caching the results of running the function so the next time you run the function it finishes almost instantly.

I am having trouble caching the responses from GitHub, though, as the caching libraries I have tried don't seem to work for PyGitHub methods like get_releases. For example, requests-cache library only works for methods in the requests library.

  • maybe you should create own cache and check value in cache before you run `get_releases()` and return value from cache. You don't have to cache `requests` – furas May 20 '22 at 01:22
  • there is standard module `functools` and it has [functools.cache](https://docs.python.org/3/library/functools.html#functools.cache) to cache execution of any function - so you could create function which gets `repo` and returns `list(repo.get_releases())` and add cache to this function. – furas May 20 '22 at 01:25
  • it seems module [cachetools](https://pypi.org/project/cachetools/) has even function to keep it on disk [shelved-cache](https://pypi.org/project/shelved-cache/) – furas May 20 '22 at 01:35
  • `requests-cache` not working for some methods is curious. If you follow the trail from [get_releases()](https://github.com/PyGithub/PyGithub/blob/001970d4a828017f704f6744a5775b4207a6523c/github/Repository.py#L3060) down to the actual request, it uses `Requester.__createConnection()`, which uses `Requester.__connectionClass`, which [wraps a requests.Session object](https://github.com/PyGithub/PyGithub/blob/master/github/Requester.py#L99). I can't find any cases where PyGithub is using a different HTTP client to send requests. Can you post an example of how you're using requests-cache? – Jordan Jun 13 '22 at 16:29

0 Answers0