Created
November 10, 2019 11:33
-
-
Save nikhilkumarsingh/1808f161c0fb8f6bb1b792c782340921 to your computer and use it in GitHub Desktop.
Parallel HTTP requests using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Parallel HTTP requests using Python!\n", | |
"\n", | |
"- [requests-futures](https://github.com/ross/requests-futures)\n", | |
"\n", | |
" `pip install requests-futures`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"from requests_futures.sessions import FuturesSession\n", | |
"from concurrent.futures import ProcessPoolExecutor" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"urls = [\n", | |
" \"https://httpbin.org/ip\",\n", | |
" \"https://httpbin.org/xml\",\n", | |
" \"https://httpbin.org/json\",\n", | |
" \"https://httpbin.org/image\",\n", | |
" \"https://httpbin.org/image/png\",\n", | |
" \"https://httpbin.org/image/jpg\",\n", | |
" \"https://httpbin.org/image/jpeg\",\n", | |
" \"https://httpbin.org/headers\",\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"3.29 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit -r 1\n", | |
"with requests.Session() as session:\n", | |
" list(map(session.get, urls))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.44 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit -r 1\n", | |
"with FuturesSession() as session:\n", | |
" futures = [session.get(url) for url in urls]\n", | |
" for future in futures:\n", | |
" future.result()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1.41 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)\n" | |
] | |
} | |
], | |
"source": [ | |
"%%timeit -r 1\n", | |
"with FuturesSession(executor=ProcessPoolExecutor(max_workers=8)) as session:\n", | |
" futures = [session.get(url) for url in urls]\n", | |
" for future in futures:\n", | |
" future.result()" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment