public
Created

  • Download Gist
slow_server.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
"""Simple server for testing code against a slow-responding server.
 
requests to '/INTEGER' are replied to with 'INTEGER',
after an artificial delay.
 
"""
import time
from tornado import web, ioloop, options, httpserver
 
options.define("port", default=8888, help="run on the given port", type=int)
options.define("delay", default=0.2, help="delay builtin to each request", type=float)
 
class SlowHandler(web.RequestHandler):
@web.asynchronous
def get(self, page):
loop = ioloop.IOLoop.instance()
loop.add_timeout(loop.time() + self.settings['delay'],
lambda : self.finish(page),
)
 
if __name__ == '__main__':
options.parse_command_line()
app = web.Application([
(r'/(\d+)', SlowHandler)
], delay=options.options.delay,
)
http_server = httpserver.HTTPServer(app)
http_server.listen(options.options.port)
ioloop.IOLoop.instance().start()
threadpool.ipynb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Using ThreadPool for concurrent downloads"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is a simple illustration of using `ThreadPool` to parallelize downloads.\n",
"Assumes that bandwidth is not the limiting factor, in which case concurrency doesn't help."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import requests\n",
"from multiprocessing.pool import ThreadPool"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Test a simple request to my slow server\n",
"It just replies to any request for `/NUMBER` with the number requested,\n",
"but the server is artificially slow in its handling of requests."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%time r = requests.get(\"http://localhost:8888/10\")\n",
"r.content"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"CPU times: user 18.1 ms, sys: 4.54 ms, total: 22.6 ms\n",
"Wall time: 224 ms\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"'10'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Our test function downloads the URL for a given ID,\n",
"and parses the result (casts str of int to int)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_data(ID):\n",
" \"\"\"function for getting data from our slow server\"\"\"\n",
" r = requests.get(\"http://localhost:8888/%i\" % ID)\n",
" return int(r.content)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now test using a threadpool to get the data, using a varying number of concurrent threads"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"IDs = range(128)\n",
"for nthreads in [1, 2, 4, 8, 16, 32]:\n",
" pool = ThreadPool(nthreads)\n",
" tic = time.time()\n",
" result = pool.map(get_data, IDs)\n",
" toc = time.time()\n",
" print \"%i threads: %3.1f seconds\" % (nthreads, toc-tic)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1 threads: 26.2 seconds\n",
"2 threads: 13.3 seconds"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"4 threads: 6.7 seconds"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"8 threads: 3.4 seconds"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"16 threads: 1.8 seconds"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"32 threads: 1.1 seconds"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 4
}
],
"metadata": {}
}
]
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.