()
| 39 | # |
| 40 | |
| 41 | def 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))))) |
no test coverage detected
searching dependent graphs…