--- a/Doc/includes/mp_pool.py +++ b/Doc/includes/mp_pool.py @@ -10,10 +10,10 @@ import random import sys + # # Functions used by test code # - def calculate(func, args): result = func(*args) return '%s says that %s%s = %s' % ( @@ -21,30 +21,36 @@ func.__name__, args, result ) + def calculatestar(args): return calculate(*args) + def mul(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a * b + def plus(a, b): - time.sleep(0.5*random.random()) + time.sleep(0.5 * random.random()) return a + b + def f(x): - return 1.0 / (x-5.0) + return 1.0 / (x - 5.0) + def pow3(x): - return x**3 + return x ** 3 + def noop(x): pass + # # Test code # - def test(): print('cpu_count() = %d\n' % multiprocessing.cpu_count()) @@ -107,9 +113,9 @@ (N, time.time() - t)) t = time.time() - C = list(pool.imap(pow3, range(N), chunksize=N//8)) + C = list(pool.imap(pow3, range(N), chunksize=N // 8)) print('\tlist(pool.imap(pow3, range(%d), chunksize=%d)):\n\t\t%s' \ - ' seconds' % (N, N//8, time.time() - t)) + ' seconds' % (N, N // 8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) print() @@ -129,9 +135,9 @@ (time.time() - t)) t = time.time() - C = list(pool.imap(noop, L, chunksize=len(L)//8)) + 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)) + (len(L) // 8, time.time() - t)) assert A == B == C, (len(A), len(B), len(C)) print()