1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2026 The Uncertainty Quantification Foundation.
# License: 3-clause BSD. The full license text is available at:
# - https://github.com/uqfoundation/pathos/blob/master/LICENSE
# pickle fails for nested functions
def adder(augend):
zero = [0]
def inner(addend):
return addend+augend+zero[0]
return inner
# build from inner function
add_me = adder(5)
# build from lambda functions
squ = lambda x:x**2
if __name__ == '__main__':
from pathos.helpers import freeze_support, shutdown
freeze_support()
from pathos.pools import ProcessPool as Pool
from pathos.pools import ThreadPool as TPool
pool = Pool()
tpool = TPool()
# test 'dilled' multiprocessing for inner
print("Evaluate 10 items on 2 proc:")
pool.ncpus = 2
print(pool)
print(pool.map(add_me, range(10)))
print('')
# test 'dilled' multiprocessing for lambda
print("Evaluate 10 items on 4 proc:")
pool.ncpus = 4
print(pool)
print(pool.map(squ, range(10)))
print('')
# test for lambda, but with threads
print("Evaluate 10 items on 4 threads:")
tpool.nthreads = 4
print(tpool)
print(tpool.map(squ, range(10)))
print('')
# shutdown all cached pools
shutdown()
# end of file
|