-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstream2.py
More file actions
55 lines (37 loc) · 1.21 KB
/
stream2.py
File metadata and controls
55 lines (37 loc) · 1.21 KB
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
# encoding: utf-8
from webob import Response
from random import randint
from time import sleep
from waitress import serve
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError
def mul(x, y):
sleep(0.125)
return ('text/plain', "{0} * {1} = {2}\n".format(x, y, x * y))
executor = ThreadPoolExecutor(10)
def app(environ, start_response):
"""Multipart AJAX request example.
See: http://test.getify.com/mpAjax/description.html
"""
response = Response()
parts = []
for i in range(12):
for j in range(12):
parts.append(executor.submit(mul, i, j))
def stream(parts, timeout=None):
try:
for future in as_completed(parts, timeout):
mime, result = future.result()
result = result.encode('utf8')
yield "!!!!!!=_NextPart_{num}\nContent-Type: {mime}\nContent-Length: {length}\n\n".format(
num = randint(100000000, 999999999),
mime = mime,
length = len(result)
).encode('utf8') + result
except TimeoutError:
for future in parts:
future.cancel()
response.content_length = None
response.app_iter = stream(parts, 0.2)
return response(environ, start_response)
if __name__ == '__main__':
serve(app, host='127.0.0.1', port=8080, threads=4)