Skip to content

Instantly share code, notes, and snippets.

@coady
Created September 28, 2015 04:09
Show Gist options
  • Save coady/f9e1be438ba8551dabad to your computer and use it in GitHub Desktop.
Save coady/f9e1be438ba8551dabad to your computer and use it in GitHub Desktop.
Parallel http requests.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HTTP requests\n",
"The [requests package](https://pypi.python.org/pypi/requests) package doesn't support sending multiple requests in parallel. Given the underlying urllib3, it would be difficult to add. However, the low-level builtin http client can accomplish it.\n",
"\n",
"Consider three strategies for comparison: serial requests (the baseline), [pipelined](https://en.wikipedia.org/wiki/HTTP_pipelining) requests, and parallel requests with multiple connections (and without threads). When reusing connections, each response must be fully read before using the next response. Generator expressions are returned so requests are sent immediately, and the timing is fair."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import httplib\n",
"host = 'httpbin.org'\n",
"requests = [('GET', '/delay/1')] * 3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 20.2 ms, sys: 1.63 ms, total: 21.8 ms\n",
"Wall time: 4.02 s\n"
]
}
],
"source": [
"def sequential(host, *requests):\n",
" \"Send requests (method, path[, body[, headers]]) and generate responses serially.\"\n",
" conn = httplib.HTTPSConnection(host)\n",
" return (conn.request(*request) or conn.getresponse() for request in requests)\n",
"\n",
"responses = sequential(host, *requests)\n",
"%time for response in responses: assert response.read()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3.69 ms, sys: 181 µs, total: 3.87 ms\n",
"Wall time: 3.23 s\n"
]
}
],
"source": [
"def pipeline(host, *requests):\n",
" \"Pipeline requests (method, path[, body[, headers]]) and generate responses.\"\n",
" conn = httplib.HTTPSConnection(host)\n",
" responses = []\n",
" for request in requests:\n",
" conn.request(*request)\n",
" responses.append(conn.response_class(conn.sock, method=conn._method))\n",
" conn._HTTPConnection__state = 'Idle'\n",
" return (response.begin() or response for response in responses)\n",
"\n",
"responses = pipeline(host, *requests)\n",
"%time for response in responses: assert response.read()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3.26 ms, sys: 152 µs, total: 3.42 ms\n",
"Wall time: 1.29 s\n"
]
}
],
"source": [
"def concurrent(host, *requests):\n",
" \"Send requests (method, path[, body[, headers]]) in parallel and generate responses.\"\n",
" conns = []\n",
" for request in requests:\n",
" conns.append(httplib.HTTPSConnection(host))\n",
" conns[-1].request(*request)\n",
" return (conn.getresponse() for conn in conns)\n",
"\n",
"responses = concurrent(host, *requests)\n",
"%time for response in responses: assert response.read()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@lafftar
Copy link

lafftar commented Nov 13, 2021

This is for python 2. If anyone else is looking for a python 3 example: urllib3/urllib3#52 (comment)

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