不能腌制功能 [英] Can't pickle Function
问题描述
所以我试图通过做一些多处理来加快我的计算时间
So I'm trying to speed up my computation time by doing a little bit multiprocessing
我正在尝试使用池工作程序.
I'm trying to use the pool workers.
我的代码顶部有
import Singal as s
import multiprocessing as mp
def wrapper(Channel):
Noise_Frequincies = []
for i in range(1,125):
Noise_Frequincies.append(60.0*float(i))
Noise_Frequincies.append(180.0)
filter1 = s.Noise_Reduction(Sample_Rate,Noise_Frequincies,Channel)
return filter1
然后当时间到时我用
Both_Channels = [Chan1, Chan2]
results = mp.Pool(2).map(wrapper,Both_Channels)
filter1 = results[0]
filter2 = results[1]
我收到以下错误
Exception in thread Thread-2:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 808, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 342, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
Chan1和Chan2是我的信号阵列,我正在尝试从每个信号中滤除一些噪声. 我是多重处理的新手,如果这是一个愚蠢的错误,我深表歉意
Chan1 and Chan2 are arrays of my signal, and I'm trying to filter out some noise out of each. I'm new to multiprocessing so I apologize if this is a dumb error
推荐答案
我将此问题标记为 Q/A的重复 a>,并总结一下:您无法腌制函数,这就是将wrapper()
传递给Pool().map()
时要尝试的操作.要腌制功能,您需要使用copy_reg,如此
I marked this question as dup of that Q/A, and to sum up: you can't pickle functions, and that's what you're trying to do when you pass wrapper()
to Pool().map()
. To pickle functions, you need to use copy_reg, as shown by this example.
无法腌制类,函数和方法-如果您腌制对象,则不会腌制对象的类,而只是识别其所属类的字符串."(cf
- http://docs.python.org/2/library/copy_reg.html?highlight=copyreg
- http://bytes.com/topic/python/answers/552476-why-cant-you-pickle-instancemethods
我不使用自定义类,而这正是他的问题所在(至少据我所知)
I don't use custom classes, and that's where his issue is (at least as far as I can tell)
不,他的问题是他试图对一个实例方法进行腌制,该实例方法与一个函数相对较近,因为不能对它们两者都进行腌制.而且该A的解决方法也应该对您有用.
no, his problem that he was trying to pickle an instance method, which is relatively close to a function, as both can't be pickled. And the workaround for that A should work for you too.
尽管我没有对其进行测试...
Though I did not test it…
HTH
这篇关于不能腌制功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!