Skip to content

Instantly share code, notes, and snippets.

@emil45
Last active May 26, 2021 19:46
Show Gist options
  • Save emil45/2c85091cbcb0b465cc354dd628424edf to your computer and use it in GitHub Desktop.
Save emil45/2c85091cbcb0b465cc354dd628424edf to your computer and use it in GitHub Desktop.
Python speed comparison between different approaches to make network requests
import threading
import multiprocessing
import asyncio
import time
import requests
import httpx
URL = "https://httpbin.org/delay/2"
NOR = 1000
def measure(func):
def wrapper(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
print(func.__name__, time.time() - start)
return wrapper
def use_requests(n=None):
response = requests.get(URL)
assert response.ok
async def use_httpx():
async with httpx.AsyncClient() as client:
response = await client.get(URL)
response.raise_for_status()
@measure
def test_requests_synchronously():
for _ in range(NOR):
use_requests()
@measure
def test_requests_using_threads():
threads = [threading.Thread(target=requests.get, args=(URL,)) for _ in range(NOR)]
for t in threads: t.start()
for t in threads: t.join()
@measure
def test_requests_using_httpx():
async_requests = [use_httpx() for _ in range(NOR)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*async_requests))
@measure
def test_requests_using_processes_with_pool_and_map_async():
with multiprocessing.Pool() as pool:
async_results = pool.map_async(use_requests, range(NOR))
async_results.wait()
@measure
def test_requests_using_processes_with_pool_and_map():
with multiprocessing.Pool() as pool:
pool.map(use_requests, range(NOR))
@measure
def test_requests_using_processes():
processes = [multiprocessing.Process(target=requests.get, args=(URL,)) for _ in range(NOR)]
for p in processes: p.start()
for p in processes: p.join()
def main():
# test_requests_synchronously()
test_requests_using_threads()
test_requests_using_processes()
test_requests_using_processes_with_pool_and_map()
test_requests_using_processes_with_pool_and_map_async()
test_requests_using_httpx()
if __name__ == '__main__':
print(f"Total Requests: {NOR}. URL: {URL} \n")
main()
@emil45
Copy link
Author

emil45 commented Feb 3, 2021

Results on a local computer (MacBook pro 12 CPU, 16 RAM):

Total Requests: 100. URL: https://httpbin.org/delay/2 

test_requests_using_threads 3.4069130420684814
test_requests_using_processes 13.154114246368408
test_requests_using_processes_with_pool_and_map 25.135432958602905
test_requests_using_processes_with_pool_and_map_async 25.549559116363525
test_requests_using_httpx 3.6979780197143555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment