-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforcemap.py
58 lines (52 loc) · 1.5 KB
/
forcemap.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
'''
Like dummy.Pool.map, but no limit of number of threads.
Useful when IO-bound.
'''
from threading import Thread, Lock, Condition
from queue import Queue, Empty
def forceMap(function, iter_input, thread_max = 4):
thread_num = 0
result_num = 0
list_output = [None] * len(iter_input)
queue = Queue()
for i, input in enumerate(iter_input):
if thread_num < thread_max:
thread_num += 1
MyThread(i, input, function, queue)
else:
id, output = queue.get()
list_output[id] = output
result_num += 1
MyThread(i, input, function, queue)
while result_num < len(list_output):
try:
id, output = queue.get(timeout=1)
except Empty:
continue
list_output[id] = output
result_num += 1
return list_output
class MyThread(Thread):
def __init__(self, id, input, function, queue):
super(__class__,self).__init__()
self.id = id
self.input = input
self.function = function
self.queue = queue
self.start()
def run(self):
output = self.function(self.input)
self.queue.put((self.id, output))
if __name__ == '__main__':
from time import sleep
def task(name):
print(name,'start')
sleep(.5)
print(name, 1)
sleep(.5)
print(name, 2)
sleep(.5)
print(name, 'END')
return name
print(forceMap(task,range(9),9))
input('EENNDD\n')