Created
November 16, 2020 11:35
-
-
Save aahnik/4a353039e07cd6ce8dacd7b003d70cb9 to your computer and use it in GitHub Desktop.
Jai shree async ... Showing the power of async. ( Idea from https://youtu.be/R4Oz8JUuM4s )
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
import aiohttp | |
import asyncio | |
from timer import timer | |
async def fetch(): | |
base = 'https://httpbin.org/get' | |
async with aiohttp.ClientSession() as session: | |
async with session.get(url=base) as response: | |
print(response.status) | |
async def main(): | |
tasks = [fetch() for i in range(100)] | |
await asyncio.gather(*tasks) | |
@timer(1, 1) | |
def func(): | |
asyncio.run(main()) | |
# took 5.891011276999961s |
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
import requests | |
from timer import timer | |
def fetch(): | |
resp = requests.get('https://httpbin.org/get') | |
print(resp.status_code) | |
@timer(1, 1) | |
def main(): | |
for i in range(100): | |
fetch() | |
# took 164.6341278480004s |
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
import timeit | |
def timer(number, repeat): | |
def wrapper(func): | |
# when func does not take any args | |
runs = timeit.repeat(func, number=number, repeat=repeat) | |
print(sum(runs) / len(runs)) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment