-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPython_Multiprocessing_Pool_Manager.py
More file actions
48 lines (31 loc) · 1.29 KB
/
Copy pathPython_Multiprocessing_Pool_Manager.py
File metadata and controls
48 lines (31 loc) · 1.29 KB
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
# Python multiprocessing Manager
# The other high-level interface, the Manager class, creates a separate server process that can hold master
# copies of Python data structures. Other processes can then access and modify these data structures using
# proxy objects. The following example creates a shared dictionary by calling the dict() method; the worker
# processes then insert values into the dictionary. (Locking is not done for you automatically, which doesn’t
# matter in this example. Manager’s methods also include Lock(), RLock(), and Semaphore() to create shared
# locks.)
import time
from multiprocessing import Pool, Manager
def factorial(N, dictionary):
"Compute a factorial."
# Calculate the result
fact = 1L
for i in range(1, N+1):
fact = fact * i
# Store result in dictionary
dictionary[N] = fact
if __name__ == '__main__':
p = Pool(5)
mgr = Manager()
d = mgr.dict() # Create shared dictionary
# Run tasks using the pool
for N in range(1, 1000, 10):
p.apply_async(factorial, (N, d))
# Mark pool as closed -- no more tasks can be added.
p.close()
# Wait for tasks to exit
p.join()
# Output results
for k, v in sorted(d.items()):
print k, v