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 57 58 59 60 61 62 63
|
#!/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
import dill
import pickle #XXX: multiprocessing needs cPickle + copy_reg
dumps = pickle._dumps
loads = pickle._loads
# pickle fails for nested functions
def adder(augend):
zero = [0]
def inner(addend):
return addend+augend+zero[0]
return inner
# test the pickle-ability of inner function
add_me = adder(5)
pinner = dumps(add_me)
p_add_me = loads(pinner)
assert add_me(10) == p_add_me(10)
# pickle fails for lambda functions
squ = lambda x:x**2
# test the pickle-ability of inner function
psqu = dumps(squ)
p_squ = loads(psqu)
assert squ(10) == p_squ(10)
if __name__ == '__main__':
from pathos.helpers import freeze_support
freeze_support()
from pathos.pools import _ProcessPool as Pool
pool = Pool()
# if pickle works, then multiprocessing should too
print("Evaluate 10 items on 2 proc:")
pool.ncpus = 2
p_res = pool.map(add_me, range(10))
print(pool)
print('%s' % p_res)
print('')
# if pickle works, then multiprocessing should too
print("Evaluate 10 items on 4 proc:")
pool.ncpus = 4
p2res = pool.map(squ, range(10))
print(pool)
print('%s' % p2res)
print('')
# shutdown the pool
pool.close()
# end of file
|