具有多处理功能的 Python 装饰器失败
我想在一个函数上使用装饰器,然后将其传递给多处理池。但是,代码失败并显示“PicklingError: Can't pickle : attribute Lookup __builtin__
.function failed”。我不太明白为什么它在这里失败。我确信这很简单,但我找不到它。下面是一个最小的“工作”示例。我认为使用 functools 函数就足以让这个工作完成。
如果我注释掉函数装饰,它就可以正常工作。我在这里误解了多处理
是什么?有什么办法可以让这个工作吗?
编辑:添加可调用类装饰器和函数装饰器后,函数装饰器按预期工作。可调用类装饰器继续失败。可调用类版本有什么问题可以防止它被腌制?
import random
import multiprocessing
import functools
class my_decorator_class(object):
def __init__(self, target):
self.target = target
try:
functools.update_wrapper(self, target)
except:
pass
def __call__(self, elements):
f = []
for element in elements:
f.append(self.target([element])[0])
return f
def my_decorator_function(target):
@functools.wraps(target)
def inner(elements):
f = []
for element in elements:
f.append(target([element])[0])
return f
return inner
@my_decorator_function
def my_func(elements):
f = []
for element in elements:
f.append(sum(element))
return f
if __name__ == '__main__':
elements = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
pool = multiprocessing.Pool(processes=4)
results = [pool.apply_async(my_func, ([e],)) for e in elements]
pool.close()
f = [r.get()[0] for r in results]
print(f)
I would like to use a decorator on a function that I will subsequently pass to a multiprocessing pool. However, the code fails with "PicklingError: Can't pickle : attribute lookup __builtin__
.function failed". I don't quite see why it fails here. I feel certain that it's something simple, but I can't find it. Below is a minimal "working" example. I thought that using the functools
function would be enough to let this work.
If I comment out the function decoration, it works without an issue. What is it about multiprocessing
that I'm misunderstanding here? Is there any way to make this work?
Edit: After adding both a callable class decorator and a function decorator, it turns out that the function decorator works as expected. The callable class decorator continues to fail. What is it about the callable class version that keeps it from being pickled?
import random
import multiprocessing
import functools
class my_decorator_class(object):
def __init__(self, target):
self.target = target
try:
functools.update_wrapper(self, target)
except:
pass
def __call__(self, elements):
f = []
for element in elements:
f.append(self.target([element])[0])
return f
def my_decorator_function(target):
@functools.wraps(target)
def inner(elements):
f = []
for element in elements:
f.append(target([element])[0])
return f
return inner
@my_decorator_function
def my_func(elements):
f = []
for element in elements:
f.append(sum(element))
return f
if __name__ == '__main__':
elements = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
pool = multiprocessing.Pool(processes=4)
results = [pool.apply_async(my_func, ([e],)) for e in elements]
pool.close()
f = [r.get()[0] for r in results]
print(f)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 pickle 需要有某种方法来重新组装你 pickle 的所有东西。请参阅此处以获取可腌制的内容的列表:
http://docs.python.org/library/pickle.html#what-can-be-pickled-and-unpickled
当 pickle
my_func
时,需要对以下组件进行 pickle:安
my_decorator_class
的实例,称为my_func
。这很好。 Pickle 将存储类的名称并腌制其 __dict__ 内容。当unpickle时,它使用名称来查找类,然后创建一个实例并填充
__dict__
内容。但是,__dict__
内容存在问题......存储在
my_func.target
中的原始my_func
实例。这不太好。它是顶层的函数,通常可以对它们进行 pickle。 Pickle 将存储函数的名称。然而,问题是名称“my_func”不再绑定到未修饰的函数,而是绑定到已修饰的函数。这意味着 pickle 将无法查找未修饰的函数来重新创建对象。遗憾的是,pickle 没有任何方法知道它尝试 pickle 的对象始终可以在名称
__main__.my_func
下找到。您可以像这样更改它,它会起作用:
您已经观察到,当类不起作用时,装饰器函数起作用。我相信这是因为 functools.wraps 修改了修饰函数,以便它具有它所包装的函数的名称和其他属性。据 pickle 模块所知,它与普通的顶级函数没有区别,因此它通过存储其名称来对其进行 pickle。取消腌制后,名称将绑定到装饰函数,因此一切正常。
The problem is that pickle needs to have some way to reassemble everything that you pickle. See here for a list of what can be pickled:
http://docs.python.org/library/pickle.html#what-can-be-pickled-and-unpickled
When pickling
my_func
, the following components need to be pickled:An instance of
my_decorator_class
, calledmy_func
.This is fine. Pickle will store the name of the class and pickle its
__dict__
contents. When unpickling, it uses the name to find the class, then creates an instance and fills in the__dict__
contents. However, the__dict__
contents present a problem...The instance of the original
my_func
that's stored inmy_func.target
.This isn't so good. It's a function at the top-level, and normally these can be pickled. Pickle will store the name of the function. The problem, however, is that the name "my_func" is no longer bound to the undecorated function, it's bound to the decorated function. This means that pickle won't be able to look up the undecorated function to recreate the object. Sadly, pickle doesn't have any way to know that object it's trying to pickle can always be found under the name
__main__.my_func
.You can change it like this and it will work:
You have observed that the decorator function works when the class does not. I believe this is because
functools.wraps
modifies the decorated function so that it has the name and other properties of the function it wraps. As far as the pickle module can tell, it is indistinguishable from a normal top-level function, so it pickles it by storing its name. Upon unpickling, the name is bound to the decorated function so everything works out.我在多处理中使用装饰器也遇到了一些问题。我不确定这是否与您的问题相同:
我的代码看起来像这样:
当我运行代码时,我得到这个:
我通过定义一个新函数将函数包装在装饰器函数中来修复它,而不是使用装饰器语法
代码运行完美,我得到:
我在 Python 方面不是很有经验,但是这个解决方案为我解决了我的问题
I also had some problem using decorators in multiprocessing. I'm not sure if it's the same problem as yours:
My code looked like this:
and when I run the code I get this:
I fixed it by defining a new function to wrap the function in the decorator function, instead of using the decorator syntax
The code ran perfectly and I got:
I'm not very experienced at Python, but this solution solved my problem for me
如果你太想要装饰器(像我一样),你也可以在函数字符串上使用 exec() 命令,来避免提到的酸洗。
我希望能够将所有参数传递给原始函数,然后连续使用它们。以下是我的代码。
首先,我创建了一个 make_functext() 函数来将目标函数对象转换为字符串。为此,我使用了
inspect
模块中的getsource()
函数(请参阅文档 此处并注意它无法从编译代码等中检索源代码)。它是这样的:它在以下将成为进程目标的
_worker()
函数中使用:最后,这是我的装饰器:
最终可以通过定义如下函数来使用代码:
...现在可以这样调用:
...输出:
感谢您的阅读,这是我的第一篇文章。如果您发现任何错误或不好的做法,请随时发表评论。我知道这些字符串转换非常脏,但是......
If you want the decorators too bad (like me), you can also use the
exec()
command on the function string, to circumvent the mentioned pickling.I wanted to be able to pass all the arguments to an original function and then use them successively. The following is my code for it.
At first, I made a
make_functext()
function to convert the target function object to a string. For that, I used thegetsource()
function from theinspect
module (see doctumentation here and note that it can't retrieve source code from compiled code etc.). Here it is:It is used in the following
_worker()
function that will be the target of the processes:And finally, here's my decorator:
The code can finally be used by defining a function like this:
... which can now be called like this:
... which outputs:
Thanks for reading, this is my very first post. If you find any mistakes or bad practices, feel free to leave a comment. I know that these string conversions are quite dirty, though...
如果您将此代码用于装饰器:
那么您可以像这样使用它,在这 4 个示例中:
输出将为:
m1 -- a=11 b='b'
m2 -- 22 cc ='cc'
c3 -- k=33 n='n'
(后跟方法 m2 中引发的异常)
If you use this code for your decorator:
Then you can use it like this, in these 4 examples:
The output will be:
m1 -- a=11 b='b'
m2 -- 22 cc='cc'
c3 -- k=33 n='n'
(followed by the exception raised in method m2)