MCPcopy Index your code
hub / github.com/python/cpython / test

Function test

Doc/includes/mp_pool.py:41–148  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39#
40
41def test():
42 PROCESSES = 4
43 print('Creating pool with %d processes\n' % PROCESSES)
44
45 with multiprocessing.Pool(PROCESSES) as pool:
46 #
47 # Tests
48 #
49
50 TASKS = [(mul, (i, 7)) for i in range(10)] + \
51 [(plus, (i, 8)) for i in range(10)]
52
53 results = [pool.apply_async(calculate, t) for t in TASKS]
54 imap_it = pool.imap(calculatestar, TASKS)
55 imap_unordered_it = pool.imap_unordered(calculatestar, TASKS)
56
57 print('Ordered results using pool.apply_async():')
58 for r in results:
59 print('\t', r.get())
60 print()
61
62 print('Ordered results using pool.imap():')
63 for x in imap_it:
64 print('\t', x)
65 print()
66
67 print('Unordered results using pool.imap_unordered():')
68 for x in imap_unordered_it:
69 print('\t', x)
70 print()
71
72 print('Ordered results using pool.map() --- will block till complete:')
73 for x in pool.map(calculatestar, TASKS):
74 print('\t', x)
75 print()
76
77 #
78 # Test error handling
79 #
80
81 print('Testing error handling:')
82
83 try:
84 print(pool.apply(f, (5,)))
85 except ZeroDivisionError:
86 print('\tGot ZeroDivisionError as expected from pool.apply()')
87 else:
88 raise AssertionError('expected ZeroDivisionError')
89
90 try:
91 print(pool.map(f, list(range(10))))
92 except ZeroDivisionError:
93 print('\tGot ZeroDivisionError as expected from pool.map()')
94 else:
95 raise AssertionError('expected ZeroDivisionError')
96
97 try:
98 print(list(pool.imap(f, list(range(10)))))

Callers 1

mp_pool.pyFile · 0.70

Calls 11

listClass · 0.85
apply_asyncMethod · 0.80
imapMethod · 0.80
imap_unorderedMethod · 0.80
PoolMethod · 0.45
getMethod · 0.45
mapMethod · 0.45
applyMethod · 0.45
flushMethod · 0.45
writeMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…