-
Notifications
You must be signed in to change notification settings - Fork 0
/
puremultiprocessing.py
52 lines (46 loc) · 1.63 KB
/
puremultiprocessing.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
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
# -*- coding: utf-8 -*
"""
This is file is bench mark for downloading all files using pure
multiprocessing. Here we dont use threads or gevents.
"""
from multiprocessing import Process, Queue, cpu_count
from requests import get
import datetime
from Queue import Empty
def download(files_to_download):
try:
while 1:
print("=== started to download ===")
start = datetime.datetime.now()
to_download = files_to_download.get_nowait()
if to_download:
dl_file = get(to_download)
if dl_file.ok:
with open(dl_file.headers['content-disposition'].split('=')[1],
'wb') as f:
f.write(dl_file.content)
print("{0} took {1} seconds".format(to_download, datetime.datetime.now(\
) - start))
except Empty:
print("queue is empty")
print("=== end ===")
def main():
files_to_download = Queue()
with open('downloads.txt', 'r') as f:
for to_download in f:
files_to_download.put_nowait(to_download.split('\n')[0])
print("=== puremultiprocessing ===")
total_processors = cpu_count()
start = datetime.datetime.now()
jobs = []
for i in range(total_processors):
p = Process(target = download, args=(files_to_download,))
jobs.append(p)
p.start()
for j in jobs:
j.join()
print("{0} took {1} seconds to complete all downloads".format(__file__,\
datetime.datetime.now() - start))
if __name__ == "__main__":
main()