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

标签 python apache-spark

以下文字摘自 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/

相关文章:

python - 使用 Python 操作 csv 文件

尝试将 Spark RDD 存储到 HBase 时出现 java.io.FileNotFoundException

apache-spark - 如何在 Spark 中处理制表符分隔的文件?

apache-spark - Apache Spark : Get the first and last row of each partition

apache-spark - pyspark-对Array(Integer())类型的列中的值求和的最佳方法

python - 在python中按多列对csv文件进行大小排序

python - 使用python读取jpeg的EXIF数据

Python 立即打印?

python - python 如何获得毫秒分辨率的时间

apache-spark - 在 Pyspark 中将时间戳更改为 UTC 格式