gpt4 book ai didi

python - 序列化包含函数的对象 - 含义

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:35 30 4
gpt4 key购买 nike

以下文字摘自 Learning Spark 第 3 章

One issue to watch out for when passing functions is inadvertently serializing the object containing the function. When you pass a function that is the member of an object, or contains references to fields in an object (e.g., self.field), Spark sends the entire object to worker nodes, which can be much larger than the bit of information you need (see Example 3-19). Sometimes this can also cause your program to fail, if your class contains objects that Python can’t figure out how to pickle.

enter image description here

请解释一下作者想在这里传达什么信息。

最佳答案

分布式系统中的工作人员没有共享内存,因此每个工作人员都必须拥有运行代码可能需要的所有函数、数据等的副本。因此,当您进行应该分布式的调用时,您应该尝试通过确保没有复制工作线程并不真正需要的内容来减少这种开销。

在此示例中,rdd.filter 是一个矢量化(函数)操作,可以分布在多个工作进程中。它需要一个参数 - 一个函数。作者警告您,如果该函数引用任何对象的字段(属性)(self 是对象用来引用自身的内部术语,那么您基本上引用了将调用 >filter 方法在它自己的方法中),整个对象(实例)将被复制到工作线程,而不仅仅是一个轻量级函数。并且该对象可以包含引用大量数据的许多属性。

关于python - 序列化包含函数的对象 - 含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281131/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com