-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_multiprocess.py
86 lines (61 loc) · 2.43 KB
/
test_multiprocess.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import multiprocessing
POISON_PILL = "STOP"
def process_odds(in_queue, shared_list):
while True:
# block until something is placed on the queue
new_value = in_queue.get()
# check to see if we just got the poison pill
if new_value == POISON_PILL:
break
# we didn't, so do the processing and put the result in the
# shared data structure
shared_list.append(new_value)
return
def process_evens(in_queue, shared_list):
while True:
new_value = in_queue.get()
if new_value == POISON_PILL:
break
shared_list.append(new_value/-2)
return
def main():
# create a manager - it lets us share native Python object types like
# lists and dictionaries without worrying about synchronization -
# the manager will take care of it
manager = multiprocessing.Manager()
# now using the manager, create our shared data structures
odd_queue = manager.Queue()
#even_queue = manager.Queue()
shared_list = manager.list()
# lastly, create our pool of workers - this spawns the processes,
# but they don't start actually doing anything yet
pool = multiprocessing.Pool()
# now we'll assign two functions to the pool for them to run -
# one to handle even numbers, one to handle odd numbers
odd_result = pool.apply_async(process_odds, (odd_queue, shared_list))
#even_result = pool.apply_async(process_evens, (even_queue, shared_list))
# this code doesn't do anything with the odd_result and even_result
# variables, but you have the flexibility to check exit codes
# and other such things if you want - see docs for AsyncResult objects
# now that the processes are running and waiting for their queues
# to have something, lets give them some work to do by iterating
# over our data, deciding who should process it, and putting it in
# their queue
for i in range(1000000):
#if (i % 2) == 0: # use mod operator to see if "i" is even
# even_queue.put(i)
#else:
odd_queue.put(i)
# now we've finished giving the processes their work, so send the
# poison pill to tell them to exit
#even_queue.put(POISON_PILL)
odd_queue.put(POISON_PILL)
# wait for them to exit
pool.close()
pool.join()
# now we can check the results
print(len(shared_list))
# ...and exit!
return
if __name__ == "__main__":
main()