Here are short examples of concurrent requests with Uplink.
Last active
November 30, 2017 14:40
-
-
Save prkumar/4e905edb988bc3d3d95e680ef043f934 to your computer and use it in GitHub Desktop.
Non-blocking IO with Uplink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of using Uplink with aiohttp for non-blocking HTTP requests. | |
This should work on Python 3.5 and above. | |
To support Python 3.4, change ``async def`` to ``def``, replace ``await`` | |
with ``yield from``, and decorate ``get_repo`` with ``asyncio.coroutine``. | |
""" | |
import asyncio | |
import uplink | |
BASE_URL = "https://api.github.com/" | |
@uplink.headers({"Accept": "application/vnd.github.v3.full+json"}) | |
class GitHub(uplink.Consumer): | |
@uplink.get("/repositories") | |
def get_repos(self): | |
"""Get all public repositories.""" | |
@uplink.get("/repos/{owner}/{repo}") | |
def get_repo(self, owner, repo): | |
"""Get a specific repository.""" | |
async def get_repo(full_name): | |
print('Getting GitHub repository `{}`'.format(full_name)) | |
response = await gh_async.get_repo(*full_name.split("/")) | |
json = await response.json() | |
print('response for {}: {}'.format(full_name, json)) | |
return json | |
if __name__ == "__main__": | |
# This consumer instance uses Requests to make blocking requests. | |
gh_sync = GitHub(base_url=BASE_URL) | |
# This uses an aiohttp, an HTTP client for asyncio. | |
gh_async = GitHub(base_url=BASE_URL, client=uplink.AiohttpClient()) | |
# First, let's fetch a list of all public repositories. | |
repos = gh_sync.get_repos().json() | |
# Use only the first 10 results to avoid hitting the rate limit. | |
repos = repos[:10] | |
# Concurrently fetch the details for those repositories. | |
futures = [get_repo(repo["full_name"]) for repo in repos] | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(asyncio.wait(futures)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of using Uplink with Twisted for non-blocking HTTP requests. | |
This should work on Python 2.7 and 3.3+. | |
""" | |
from twisted.internet import reactor, defer | |
import uplink | |
BASE_URL = "https://api.github.com/" | |
@uplink.headers({"Accept": "application/vnd.github.v3.full+json"}) | |
class GitHub(uplink.Consumer): | |
@uplink.get("/repositories") | |
def get_repos(self): | |
"""Get all public repositories.""" | |
@uplink.get("/repos/{owner}/{repo}") | |
def get_repo(self, owner, repo): | |
"""Get a specific repository.""" | |
@defer.inlineCallbacks | |
def get_repo(full_name): | |
print('Getting GitHub repository `{}`'.format(full_name)) | |
response = yield gh_async.get_repo(*full_name.split("/")) | |
json = response.json() | |
print('response for {}: {}'.format(full_name, json)) | |
if __name__ == "__main__": | |
# This consumer instance uses Requests to make blocking requests. | |
gh_sync = GitHub(base_url=BASE_URL) | |
# This uses Twisted with Requests, inspired by `requests-threads`. | |
gh_async = GitHub(base_url=BASE_URL, client=uplink.TwistedClient()) | |
# First, let's fetch a list of all public repositories. | |
repos = gh_sync.get_repos().json() | |
# Use only the first 10 results to avoid hitting the rate limit. | |
repos = repos[:10] | |
# Asynchronously fetch the details for those 10 repositories. | |
deferred = [get_repo(repo["full_name"]) for repo in repos] | |
reactor.callLater(2, reactor.stop) # Stop the reactor after 2 secs | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment