Skip to content

Instantly share code, notes, and snippets.

@jackypanster
Forked from nhumrich/async_http_benchmark.py
Created December 2, 2018 01:40
Show Gist options
  • Save jackypanster/b1fc73ae07f39e6d2b7dc0ab050b5e8a to your computer and use it in GitHub Desktop.
Save jackypanster/b1fc73ae07f39e6d2b7dc0ab050b5e8a to your computer and use it in GitHub Desktop.
async vs threading http benchmark
from timeit import timeit
import asyncio
import requests
from threading import Thread
import aiohttp
client = aiohttp.ClientSession()
def call_threaded():
threads = [Thread(target=call_google_normal) for i in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
def call_async():
responses = [call_google() for i in range(10)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(responses))
async def call_google():
async with client.get('http://google.com') as resp:
return resp.status
def call_google_normal():
resp = requests.get('http://google.com')
return resp.status_code
def call_normal():
responses = [call_google_normal() for i in range(10)]
def myfunc(a):
a = ' '.join(['hello', a])
num = 5
a = timeit(call_normal, number=num)
b = timeit(call_async, number=num)
c = timeit(call_threaded, number=num)
print('normal:', a)
print('async:', b)
print('threaded:', c)
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment