Created
February 20, 2019 15:10
-
-
Save c-goosen/c035e1fa24cdb354fa7b2d919f0bcb98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# install requests | |
import requests | |
# install asyncio - also need python 3.5+ | |
import asyncio | |
from datetime import datetime | |
# install aiohttp | |
import aiohttp | |
test_url= 'http://slowwly.robertomurray.co.uk/delay/10000/url/http://google.com' | |
def wrap_requests(): | |
return requests.get(url=test_url).status_code | |
async def wrap_requests_async(session): | |
req = await session.get(url=test_url) | |
status = req.status | |
req.close() | |
return status | |
def run_fifty_times(): | |
responses = list() | |
for i in range(0, 50): | |
responses.append(wrap_requests()) | |
print(responses) | |
async def run_fifty_times_async(): | |
session = aiohttp.ClientSession() | |
requests = [wrap_requests_async(session)] * 50 | |
responses = await asyncio.gather(*requests) | |
print(responses) | |
await session.close() | |
if __name__ == "__main__": | |
loop = asyncio.get_event_loop() | |
start_time = datetime.now() | |
run_fifty_times() | |
print(datetime.now() - start_time) | |
start_time = datetime.now() | |
loop.run_until_complete(run_fifty_times_async()) | |
print(datetime.now() - start_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment