-
Notifications
You must be signed in to change notification settings - Fork 0
/
downloader2.py
executable file
·42 lines (34 loc) · 1.21 KB
/
downloader2.py
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
#!/usr/bin/python2.7
import futures
from concurrent.futures import ThreadPoolExecutor
from requests_futures.sessions import FuturesSession
def main():
url = 'https://s3.amazonaws.com/pydemo/test.zip'
async_downloads = []
session = FuturesSession(executor=ThreadPoolExecutor(max_workers=4))
for cnt in range(4):
print "getting %d..." % cnt
def cb(sess, resp, n=cnt):
fn = '/tmp/%d-test-futures.zip' % n
print "writing %s" % fn
write_part(fn, resp)
return "done with %s" % fn
s = session.get(url,
stream=True,
timeout=10, #!
background_callback=cb)
async_downloads.append(s)
for f in futures.as_completed(async_downloads):
exp = f.exception()
if exp is not None:
raise exp
print f.result()
def write_part(download_name, response):
with open(download_name, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
# filter out keep-alive new chunks
if chunk:
f.write(chunk)
f.flush()
if __name__ == '__main__':
main()