diff options
author | Christian Heimes <christian@cheimes.de> | 2008-11-28 11:23:26 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-11-28 11:23:26 (GMT) |
commit | aae1b70a83094314043ad65fc098b2aa7137c294 (patch) | |
tree | a78a27e7d4a5090810b337ee2ff6160c98a91c23 /Doc/includes/mp_pool.py | |
parent | 94e0772989073d207855a787a59f863f79230771 (diff) | |
download | cpython-aae1b70a83094314043ad65fc098b2aa7137c294.zip cpython-aae1b70a83094314043ad65fc098b2aa7137c294.tar.gz cpython-aae1b70a83094314043ad65fc098b2aa7137c294.tar.bz2 |
2to3 run of multiprocessing examples.
mp_benchmarks, mp_newtypes and mp_distribution are still broken but the others are working properly. We should include the examples in our unit test suite ...
Diffstat (limited to 'Doc/includes/mp_pool.py')
-rw-r--r-- | Doc/includes/mp_pool.py | 142 |
1 files changed, 71 insertions, 71 deletions
diff --git a/Doc/includes/mp_pool.py b/Doc/includes/mp_pool.py index e7aaaac..c9f5815 100644 --- a/Doc/includes/mp_pool.py +++ b/Doc/includes/mp_pool.py @@ -43,17 +43,17 @@ def noop(x): # def test(): - print 'cpu_count() = %d\n' % multiprocessing.cpu_count() + print('cpu_count() = %d\n' % multiprocessing.cpu_count()) # # Create pool # PROCESSES = 4 - print 'Creating pool with %d processes\n' % PROCESSES + print('Creating pool with %d processes\n' % PROCESSES) pool = multiprocessing.Pool(PROCESSES) - print 'pool = %s' % pool - print + print('pool = %s' % pool) + print() # # Tests @@ -66,72 +66,72 @@ def test(): imap_it = pool.imap(calculatestar, TASKS) imap_unordered_it = pool.imap_unordered(calculatestar, TASKS) - print 'Ordered results using pool.apply_async():' + print('Ordered results using pool.apply_async():') for r in results: - print '\t', r.get() - print + print('\t', r.get()) + print() - print 'Ordered results using pool.imap():' + print('Ordered results using pool.imap():') for x in imap_it: - print '\t', x - print + print('\t', x) + print() - print 'Unordered results using pool.imap_unordered():' + print('Unordered results using pool.imap_unordered():') for x in imap_unordered_it: - print '\t', x - print + print('\t', x) + print() - print 'Ordered results using pool.map() --- will block till complete:' + print('Ordered results using pool.map() --- will block till complete:') for x in pool.map(calculatestar, TASKS): - print '\t', x - print + print('\t', x) + print() # # Simple benchmarks # N = 100000 - print 'def pow3(x): return x**3' + print('def pow3(x): return x**3') t = time.time() - A = map(pow3, xrange(N)) - print '\tmap(pow3, xrange(%d)):\n\t\t%s seconds' % \ - (N, time.time() - t) + A = list(map(pow3, range(N))) + print('\tmap(pow3, xrange(%d)):\n\t\t%s seconds' % \ + (N, time.time() - t)) t = time.time() - B = pool.map(pow3, xrange(N)) - print '\tpool.map(pow3, xrange(%d)):\n\t\t%s seconds' % \ - (N, time.time() - t) + B = pool.map(pow3, range(N)) + print('\tpool.map(pow3, xrange(%d)):\n\t\t%s seconds' % \ + (N, time.time() - t)) t = time.time() - C = list(pool.imap(pow3, xrange(N), chunksize=N//8)) - print '\tlist(pool.imap(pow3, xrange(%d), chunksize=%d)):\n\t\t%s' \ - ' seconds' % (N, N//8, time.time() - t) + C = list(pool.imap(pow3, range(N), chunksize=N//8)) + print('\tlist(pool.imap(pow3, xrange(%d), chunksize=%d)):\n\t\t%s' \ + ' seconds' % (N, N//8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) - print + print() L = [None] * 1000000 - print 'def noop(x): pass' - print 'L = [None] * 1000000' + print('def noop(x): pass') + print('L = [None] * 1000000') t = time.time() - A = map(noop, L) - print '\tmap(noop, L):\n\t\t%s seconds' % \ - (time.time() - t) + A = list(map(noop, L)) + print('\tmap(noop, L):\n\t\t%s seconds' % \ + (time.time() - t)) t = time.time() B = pool.map(noop, L) - print '\tpool.map(noop, L):\n\t\t%s seconds' % \ - (time.time() - t) + print('\tpool.map(noop, L):\n\t\t%s seconds' % \ + (time.time() - t)) t = time.time() C = list(pool.imap(noop, L, chunksize=len(L)//8)) - print '\tlist(pool.imap(noop, L, chunksize=%d)):\n\t\t%s seconds' % \ - (len(L)//8, time.time() - t) + print('\tlist(pool.imap(noop, L, chunksize=%d)):\n\t\t%s seconds' % \ + (len(L)//8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) - print + print() del A, B, C, L @@ -139,33 +139,33 @@ def test(): # Test error handling # - print 'Testing error handling:' + print('Testing error handling:') try: - print pool.apply(f, (5,)) + print(pool.apply(f, (5,))) except ZeroDivisionError: - print '\tGot ZeroDivisionError as expected from pool.apply()' + print('\tGot ZeroDivisionError as expected from pool.apply()') else: - raise AssertionError, 'expected ZeroDivisionError' + raise AssertionError('expected ZeroDivisionError') try: - print pool.map(f, range(10)) + print(pool.map(f, list(range(10)))) except ZeroDivisionError: - print '\tGot ZeroDivisionError as expected from pool.map()' + print('\tGot ZeroDivisionError as expected from pool.map()') else: - raise AssertionError, 'expected ZeroDivisionError' + raise AssertionError('expected ZeroDivisionError') try: - print list(pool.imap(f, range(10))) + print(list(pool.imap(f, list(range(10))))) except ZeroDivisionError: - print '\tGot ZeroDivisionError as expected from list(pool.imap())' + print('\tGot ZeroDivisionError as expected from list(pool.imap())') else: - raise AssertionError, 'expected ZeroDivisionError' + raise AssertionError('expected ZeroDivisionError') - it = pool.imap(f, range(10)) + it = pool.imap(f, list(range(10))) for i in range(10): try: - x = it.next() + x = next(it) except ZeroDivisionError: if i == 5: pass @@ -173,17 +173,17 @@ def test(): break else: if i == 5: - raise AssertionError, 'expected ZeroDivisionError' + raise AssertionError('expected ZeroDivisionError') assert i == 9 - print '\tGot ZeroDivisionError as expected from IMapIterator.next()' - print + print('\tGot ZeroDivisionError as expected from IMapIterator.next()') + print() # # Testing timeouts # - print 'Testing ApplyResult.get() with timeout:', + print('Testing ApplyResult.get() with timeout:', end=' ') res = pool.apply_async(calculate, TASKS[0]) while 1: sys.stdout.flush() @@ -192,10 +192,10 @@ def test(): break except multiprocessing.TimeoutError: sys.stdout.write('.') - print - print + print() + print() - print 'Testing IMapIterator.next() with timeout:', + print('Testing IMapIterator.next() with timeout:', end=' ') it = pool.imap(calculatestar, TASKS) while 1: sys.stdout.flush() @@ -205,14 +205,14 @@ def test(): break except multiprocessing.TimeoutError: sys.stdout.write('.') - print - print + print() + print() # # Testing callback # - print 'Testing callback:' + print('Testing callback:') A = [] B = [56, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729] @@ -220,13 +220,13 @@ def test(): r = pool.apply_async(mul, (7, 8), callback=A.append) r.wait() - r = pool.map_async(pow3, range(10), callback=A.extend) + r = pool.map_async(pow3, list(range(10)), callback=A.extend) r.wait() if A == B: - print '\tcallbacks succeeded\n' + print('\tcallbacks succeeded\n') else: - print '\t*** callbacks failed\n\t\t%s != %s\n' % (A, B) + print('\t*** callbacks failed\n\t\t%s != %s\n' % (A, B)) # # Check there are no outstanding tasks @@ -238,7 +238,7 @@ def test(): # Check close() methods # - print 'Testing close():' + print('Testing close():') for worker in pool._pool: assert worker.is_alive() @@ -252,13 +252,13 @@ def test(): for worker in pool._pool: assert not worker.is_alive() - print '\tclose() succeeded\n' + print('\tclose() succeeded\n') # # Check terminate() method # - print 'Testing terminate():' + print('Testing terminate():') pool = multiprocessing.Pool(2) DELTA = 0.1 @@ -270,13 +270,13 @@ def test(): for worker in pool._pool: assert not worker.is_alive() - print '\tterminate() succeeded\n' + print('\tterminate() succeeded\n') # # Check garbage collection # - print 'Testing garbage collection:' + print('Testing garbage collection:') pool = multiprocessing.Pool(2) DELTA = 0.1 @@ -291,7 +291,7 @@ def test(): for worker in processes: assert not worker.is_alive() - print '\tgarbage collection succeeded\n' + print('\tgarbage collection succeeded\n') if __name__ == '__main__': @@ -300,12 +300,12 @@ if __name__ == '__main__': assert len(sys.argv) in (1, 2) if len(sys.argv) == 1 or sys.argv[1] == 'processes': - print ' Using processes '.center(79, '-') + print(' Using processes '.center(79, '-')) elif sys.argv[1] == 'threads': - print ' Using threads '.center(79, '-') + print(' Using threads '.center(79, '-')) import multiprocessing.dummy as multiprocessing else: - print 'Usage:\n\t%s [processes | threads]' % sys.argv[0] + print('Usage:\n\t%s [processes | threads]' % sys.argv[0]) raise SystemExit(2) test() |