以下文字摘自 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.
请解释一下作者想在这里传达什么信息。
最佳答案
分布式系统中的工作人员没有共享内存,因此每个工作人员都必须拥有运行代码可能需要的所有函数、数据等的副本。因此,当您进行应该分布式的调用时,您应该尝试通过确保没有复制工作线程并不真正需要的内容来减少这种开销。
在此示例中,rdd.filter
是一个矢量化(函数)操作,可以分布在多个工作进程中。它需要一个参数 - 一个函数。作者警告您,如果该函数引用任何对象的字段(属性)(self
是对象用来引用自身的内部术语,那么您基本上引用了将调用 >filter
方法在它自己的方法中),整个对象(实例)将被复制到工作线程,而不仅仅是一个轻量级函数。并且该对象可以包含引用大量数据的许多属性。
关于python - 序列化包含函数的对象 - 含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281131/