-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters.py
67 lines (54 loc) · 1.68 KB
/
counters.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
66
#!/usr/bin/env python3
import multiprocessing as mp
class Counter:
def __init__(self, work):
self.n = 0
self.work = work
def incr(self, upto):
for i in range(upto):
self.n += 1
def do_work(self):
for job in self.work:
self.incr(job)
class StupidLockingCounter(Counter):
def __init__(self, work):
self._lock = mp.Lock()
super().__init__(work)
def do_work(self):
for job in self.work:
self._lock.acquire()
self.incr(job)
self._lock.release()
class UnsharedConcurrentCounter(Counter):
def __init__(self, work):
super().__init__(work)
def do_work(self, f):
self.workers = mp.Pool(processes=len(self.work))
self.workers.map(f, self.work)
self.workers.close()
class SharedConcurrentCounter(Counter):
def __init__(self, work, worker_count):
self._work_queue = mp.Queue()
self.worker_count = worker_count
super().__init__(work)
for job in self.work:
self._work_queue.put(job)
self._workers = [
mp.Process(target=self.incr,
args=(self._work_queue,)) for n in range(worker_count)
]
def incr(self, q):
n = 0
if not q.empty():
while not q.empty():
job = q.get()
for i in range(job):
n += 1
def do_work(self):
tuple(map(lambda x: x.start(), self._workers))
tuple(map(lambda x: x.terminate(), [p for p in self._workers if not p.is_alive()]))
def incr(upto):
n = 0
for i in range(upto):
n += 1
return n