blob: 11101e1a90954e8a2cced8ac609844db6cf8acaa [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001import multiprocessing
2import time
3import random
4import sys
5
6#
7# Functions used by test code
8#
9
10def calculate(func, args):
11 result = func(*args)
12 return '%s says that %s%s = %s' % (
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +000013 multiprocessing.current_process().name,
Benjamin Petersone711caf2008-06-11 16:44:04 +000014 func.__name__, args, result
15 )
16
17def calculatestar(args):
18 return calculate(*args)
19
20def mul(a, b):
Raymond Hettinger40fc59d2011-04-26 13:55:55 -070021 time.sleep(0.5 * random.random())
Benjamin Petersone711caf2008-06-11 16:44:04 +000022 return a * b
23
24def plus(a, b):
Raymond Hettinger40fc59d2011-04-26 13:55:55 -070025 time.sleep(0.5 * random.random())
Benjamin Petersone711caf2008-06-11 16:44:04 +000026 return a + b
27
28def f(x):
Raymond Hettinger40fc59d2011-04-26 13:55:55 -070029 return 1.0 / (x - 5.0)
Benjamin Petersone711caf2008-06-11 16:44:04 +000030
31def pow3(x):
Raymond Hettinger40fc59d2011-04-26 13:55:55 -070032 return x ** 3
Benjamin Petersone711caf2008-06-11 16:44:04 +000033
34def noop(x):
35 pass
36
37#
38# Test code
39#
40
41def test():
Benjamin Petersone711caf2008-06-11 16:44:04 +000042 PROCESSES = 4
Christian Heimesaae1b702008-11-28 11:23:26 +000043 print('Creating pool with %d processes\n' % PROCESSES)
Benjamin Petersone711caf2008-06-11 16:44:04 +000044
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010045 with multiprocessing.Pool(PROCESSES) as pool:
46 #
47 # Tests
48 #
Benjamin Petersone711caf2008-06-11 16:44:04 +000049
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010050 TASKS = [(mul, (i, 7)) for i in range(10)] + \
51 [(plus, (i, 8)) for i in range(10)]
Benjamin Petersone711caf2008-06-11 16:44:04 +000052
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010053 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)
Benjamin Petersone711caf2008-06-11 16:44:04 +000056
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010057 print('Ordered results using pool.apply_async():')
58 for r in results:
59 print('\t', r.get())
60 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +000061
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010062 print('Ordered results using pool.imap():')
63 for x in imap_it:
64 print('\t', x)
65 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +000066
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010067 print('Unordered results using pool.imap_unordered():')
68 for x in imap_unordered_it:
69 print('\t', x)
70 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +000071
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010072 print('Ordered results using pool.map() --- will block till complete:')
73 for x in pool.map(calculatestar, TASKS):
74 print('\t', x)
75 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +000076
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010077 #
78 # Test error handling
79 #
Benjamin Petersone711caf2008-06-11 16:44:04 +000080
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010081 print('Testing error handling:')
Benjamin Petersone711caf2008-06-11 16:44:04 +000082
Benjamin Petersone711caf2008-06-11 16:44:04 +000083 try:
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010084 print(pool.apply(f, (5,)))
Benjamin Petersone711caf2008-06-11 16:44:04 +000085 except ZeroDivisionError:
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010086 print('\tGot ZeroDivisionError as expected from pool.apply()')
Benjamin Petersone711caf2008-06-11 16:44:04 +000087 else:
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010088 raise AssertionError('expected ZeroDivisionError')
Benjamin Petersone711caf2008-06-11 16:44:04 +000089
Benjamin Petersone711caf2008-06-11 16:44:04 +000090 try:
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010091 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')
Benjamin Petersone711caf2008-06-11 16:44:04 +000096
Benjamin Petersone711caf2008-06-11 16:44:04 +000097 try:
Richard Oudkerk84ed9a62013-08-14 15:35:41 +010098 print(list(pool.imap(f, list(range(10)))))
99 except ZeroDivisionError:
100 print('\tGot ZeroDivisionError as expected from list(pool.imap())')
101 else:
102 raise AssertionError('expected ZeroDivisionError')
Benjamin Petersone711caf2008-06-11 16:44:04 +0000103
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100104 it = pool.imap(f, list(range(10)))
105 for i in range(10):
106 try:
107 x = next(it)
108 except ZeroDivisionError:
109 if i == 5:
110 pass
111 except StopIteration:
112 break
113 else:
114 if i == 5:
115 raise AssertionError('expected ZeroDivisionError')
Benjamin Petersone711caf2008-06-11 16:44:04 +0000116
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100117 assert i == 9
118 print('\tGot ZeroDivisionError as expected from IMapIterator.next()')
119 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000120
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100121 #
122 # Testing timeouts
123 #
Benjamin Petersone711caf2008-06-11 16:44:04 +0000124
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100125 print('Testing ApplyResult.get() with timeout:', end=' ')
126 res = pool.apply_async(calculate, TASKS[0])
127 while 1:
128 sys.stdout.flush()
129 try:
130 sys.stdout.write('\n\t%s' % res.get(0.02))
131 break
132 except multiprocessing.TimeoutError:
133 sys.stdout.write('.')
134 print()
135 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000136
Richard Oudkerk84ed9a62013-08-14 15:35:41 +0100137 print('Testing IMapIterator.next() with timeout:', end=' ')
138 it = pool.imap(calculatestar, TASKS)
139 while 1:
140 sys.stdout.flush()
141 try:
142 sys.stdout.write('\n\t%s' % it.next(0.02))
143 except StopIteration:
144 break
145 except multiprocessing.TimeoutError:
146 sys.stdout.write('.')
147 print()
148 print()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000149
150
151if __name__ == '__main__':
152 multiprocessing.freeze_support()
Benjamin Petersone711caf2008-06-11 16:44:04 +0000153 test()