-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.py
More file actions
40 lines (34 loc) · 1008 Bytes
/
parallel.py
File metadata and controls
40 lines (34 loc) · 1008 Bytes
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
from threading import Thread
from threading import Lock
def _do_working_thread(lock, iterator, func, callback, args):
while True:
try:
lock.acquire()
item = iterator.next()
except StopIteration:
break
finally:
lock.release()
ret, data = func(item, args)
callback(item, ret, args, data)
def parallel_map(array, func, callback, args, thread_num):
iterator = iter(array)
threads = []
lock = Lock()
for i in range(thread_num):
th = Thread(target = _do_working_thread, args = (lock, iterator, func, callback, args))
th.start()
threads.append(th)
for th in threads:
th.join()
if __name__ == '__main__':
def func(item ,args):
print item
import time
print item
time.sleep(1)
return True, None
def callback(item, ret, args, data):
print item, ret
l = range(10)
parallel_map(l, func, callback, None, 2)