Skip to content

Instantly share code, notes, and snippets.

@chaseliu
Created June 6, 2018 01:42
Show Gist options
  • Save chaseliu/23fca7f987ab7083e0dc696b5ddc9750 to your computer and use it in GitHub Desktop.
Save chaseliu/23fca7f987ab7083e0dc696b5ddc9750 to your computer and use it in GitHub Desktop.
Demonstrate how to execute parallel http get requests with requests in asyncio
"""Demonstrate how to execute parallel http get requests with requests in asyncio"""
import asyncio
import concurrent.futures
import requests
num_requests = 20
# Example 1: synchronous requests
def example_1():
responses = [
requests.get('http://example.org/')
for i in range(num_requests)
]
# Example 2: asynchronous requests
def example_2():
async def main():
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
None,
requests.get,
'http://example.org/'
)
for i in range(num_requests)
]
for response in await asyncio.gather(*futures):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# Example 3: asynchronous requests with larger thread pool
def example_3():
async def main():
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
executor,
requests.get,
'http://example.org/'
)
for i in range(num_requests)
]
for response in await asyncio.gather(*futures):
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment