酸洗错误:不能腌制<type'function'=“”> </type>

时间:2014-07-24 00:18:53

标签: python numpy multiprocessing pickle core

我想知道这个错误可能意味着什么:

PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

我知道它与使用多个核心有关。我在一个集群上运行我的程序,并在我的代码行中使用了15个线程:

gauss2 = PTSampler(ntemps, renwalkers, rendim, lnlike, lnprior, threads=15)

有问题的采样器是http://dan.iel.fm/emcee/current/user/pt/

处的并行回火采样器记录的采样器

知道这个错误可能意味着什么吗?

1 个答案:

答案 0 :(得分:4)

错误意味着你试图挑选内置FunctionType ...而不是功能本身。它可能会在某处拾取函数类而不是函数本身时发生编码错误。

>>> import sys
>>> import pickle
>>> import types
>>> types.FunctionType
<type 'function'>
>>> try:
...     pickle.dumps(types.FunctionType)
... except:
...     print sys.exc_info()[1]
... 
Can't pickle <type 'function'>: it's not found as __builtin__.function
>>> def foo(x):
...   return x
... 
>>> try:
...     pickle.dumps(type(foo))
... except:
...     print sys.exc_info()[1]
... 
Can't pickle <type 'function'>: it's not found as __builtin__.function
>>> try:
...     pickle.dumps(foo.__class__)
... except:
...     print sys.exc_info()[1]
... 
Can't pickle <type 'function'>: it's not found as __builtin__.function
>>> pickle.dumps(foo)
'c__main__\nfoo\np0\n.'
>>> pickle.dumps(foo, -1)
'\x80\x02c__main__\nfoo\nq\x00.'

如果您有一个FunctionType对象,那么您需要做的就是获取该类的一个实例 - 即像foo这样的函数。