Skip to content

Instantly share code, notes, and snippets.

@aahnik
Created November 16, 2020 11:35
Show Gist options
  • Save aahnik/4a353039e07cd6ce8dacd7b003d70cb9 to your computer and use it in GitHub Desktop.
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 )
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
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
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