├── .gitignore ├── README.md ├── requirements.txt ├── test_asyncio.py ├── test_multiprocessing.py ├── test_multithreading.py ├── test_synchronous.py └── timer.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv/ 2 | .idea/ 3 | __pycache__/ 4 | 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # async-http-requests-tut 2 | 3 | Making multiple HTTP requests using Python (synchronous, multiprocessing, multithreading, asyncio) 4 | 5 | Watch tutorial video [here](https://www.youtube.com/watch?v=R4Oz8JUuM4s). 6 | 7 | 8 | More tutorials: 9 | - [Multiprocessing in Python](https://www.youtube.com/watch?v=Ju4xkvFm07o&list=PLyb_C2HpOQSDUh4kIJnprJjh5n5Wqsww8 10 | ) 11 | - [Multithreading in Python](https://www.youtube.com/watch?v=ZPM8TCz5cd8&list=PLyb_C2HpOQSC-Ncui9S4ncUdaGI2YEhwK) 12 | 13 | - [Concurrent Programming in Python (asyncio)](https://www.youtube.com/watch?v=y85G7GLYhYA&list=PLyb_C2HpOQSBsygWeCYkJ7wjxXShIql43) 14 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests 2 | aiohttp 3 | -------------------------------------------------------------------------------- /test_asyncio.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | 3 | import aiohttp 4 | 5 | from timer import timer 6 | 7 | URL = 'https://httpbin.org/uuid' 8 | 9 | 10 | async def fetch(session, url): 11 | async with session.get(url) as response: 12 | json_response = await response.json() 13 | print(json_response['uuid']) 14 | 15 | 16 | async def main(): 17 | async with aiohttp.ClientSession() as session: 18 | tasks = [fetch(session, URL) for _ in range(100)] 19 | await asyncio.gather(*tasks) 20 | 21 | 22 | @timer(1, 5) 23 | def func(): 24 | asyncio.run(main()) 25 | -------------------------------------------------------------------------------- /test_multiprocessing.py: -------------------------------------------------------------------------------- 1 | from multiprocessing.pool import Pool 2 | 3 | import requests 4 | 5 | from timer import timer 6 | 7 | URL = 'https://httpbin.org/uuid' 8 | 9 | 10 | def fetch(session, url): 11 | with session.get(url) as response: 12 | print(response.json()['uuid']) 13 | 14 | 15 | @timer(1, 1) 16 | def main(): 17 | with Pool() as pool: 18 | with requests.Session() as session: 19 | pool.starmap(fetch, [(session, URL) for _ in range(100)]) 20 | -------------------------------------------------------------------------------- /test_multithreading.py: -------------------------------------------------------------------------------- 1 | from concurrent.futures import ThreadPoolExecutor 2 | 3 | import requests 4 | 5 | from timer import timer 6 | 7 | URL = 'https://httpbin.org/uuid' 8 | 9 | 10 | def fetch(session, url): 11 | with session.get(url) as response: 12 | print(response.json()['uuid']) 13 | 14 | 15 | @timer(1, 5) 16 | def main(): 17 | with ThreadPoolExecutor(max_workers=100) as executor: 18 | with requests.Session() as session: 19 | executor.map(fetch, [session] * 100, [URL] * 100) 20 | executor.shutdown(wait=True) 21 | -------------------------------------------------------------------------------- /test_synchronous.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | from timer import timer 4 | 5 | URL = 'https://httpbin.org/uuid' 6 | 7 | 8 | def fetch(session, url): 9 | with session.get(url) as response: 10 | print(response.json()['uuid']) 11 | 12 | 13 | @timer(1, 1) 14 | def main(): 15 | with requests.Session() as session: 16 | for _ in range(100): 17 | fetch(session, URL) 18 | -------------------------------------------------------------------------------- /timer.py: -------------------------------------------------------------------------------- 1 | import timeit 2 | 3 | 4 | def timer(number, repeat): 5 | def wrapper(func): 6 | runs = timeit.repeat(func, number=number, repeat=repeat) 7 | print(sum(runs) / len(runs)) 8 | 9 | return wrapper 10 | --------------------------------------------------------------------------------