-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfutures_mapreduce.py
65 lines (48 loc) · 1.7 KB
/
futures_mapreduce.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
53
54
55
56
57
58
59
60
61
62
63
64
65
from collections import defaultdict
from concurrent.futures import ProcessPoolExecutor as Executor
from time import sleep
def report_progress(futures, tag, callback):
not_done = 1
done = 0
while not_done > 0:
not_done = 0
done = 0
for fut in futures:
if fut.done():
done +=1
else:
not_done += 1
sleep(0.5)
if callback:
callback(tag, done, not_done)
def async_map(executor, mapper, data):
futures = []
for datum in data:
futures.append(executor.submit(mapper, datum))
return futures
def map_less_naive(executor, my_input, mapper):
map_results = async_map(executor, mapper, my_input)
return map_results
def map_reduce_less_naive(my_input, mapper, reducer, callback=None):
with Executor(max_workers=2) as executor:
futures = async_map(executor, mapper, my_input)
report_progress(futures, 'map', callback)
#wait(futures).done
map_results = map(lambda f: f.result(), futures)
distributor = defaultdict(list)
for key, value in map_results:
distributor[key].append(value)
futures = async_map(executor, reducer, distributor.items())
report_progress(futures, 'reduce', callback)
results = map(lambda f: f.result(), futures)
return results
def emitter(word):
return word, 1
def counter(emitted):
return emitted[0], sum(emitted[1])
def reporter(tag, done, not_done):
print(f'Operation {tag}: {done}/{done+not_done}')
words = 'Python is great Python rocks'.split(' ')
a = map_reduce_less_naive(words, emitter, counter, reporter)
for i in sorted(a, key=lambda x: x[1]):
print(i)