Skip to content

Instantly share code, notes, and snippets.

@nhumrich
Created November 3, 2016 03:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nhumrich/b1ec570d1b6226f169c4b42a2905ee88 to your computer and use it in GitHub Desktop.
Save nhumrich/b1ec570d1b6226f169c4b42a2905ee88 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