什么时候使用Dask集合的pickling方法?

问题描述 投票:0回答:1

Dask集合提供的腌制方法如 DataFramesArrays. 虽然IIUC集合本身并没有传输给工人。而是对图进行提取、优化,并将其转化为任务,然后在worker上进行(将Pandas和NumPy数组等数据依赖关系序列化)。那么,Dask集合的这些pickling方法到底是在什么情况下使用的呢?

dask
1个回答
0
投票

Pickling,或者更一般的序列化,主要是在通信过程中使用。 每当数据在工作者之间或从客户端到工作者之间传递时,数据必须转换为字节序列,这些字节可以在电线上移动,并在没有任何数据损失的情况下重新组成。

对于DataFrames,Dask使用了pickle,事实上,大多数东西都使用了pickle。

In [4]: from distributed.protocol import serialize

In [5]: serialize(pd.DataFrame({'a': [0,1,2]}))
Out[5]:
({'serializer': 'pickle'},
 [b"\x80\x04\x95\xbc\x02\x00\x00\x00\x00\x00\x00\x8c\x11pandas.core.frame\x94\x8c\tDataFrame\x94\x93\x94)\x81\x94}\x94(\x8c\x05_data\x94\x8c\x1epandas.core.internals.managers\x94\x8c\x0cBlockManager\x94\x93\x94)\x81\x94(]\x94(\x8c\x18pandas.core.indexes.base\x94\x8c\n_new_Index\x94\x93\x94h\x0b\x8c\x05Index\x94\x93\x94}\x94(\x8c\x04data\x94\x8c\x15numpy.core.multiarray\x94\x8c\x0c_reconstruct\x94\x93\x94\x8c\x05numpy\x94\x8c\x07ndarray\x94\x93\x94K\x00\x85\x94C\x01b\x94\x87\x94R\x94(K\x01K\x01\x85\x94h\x15\x8c\x05dtype\x94\x93\x94\x8c\x02O8\x94K\x00K\x01\x87\x94R\x94(K\x03\x8c\x01|\x94NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK?t\x94b\x89]\x94\x8c\x01a\x94at\x94b\x8c\x04name\x94Nu\x86\x94R\x94h\r\x8c\x19pandas.core.indexes.range\x94\x8c\nRangeIndex\x94\x93\x94}\x94(h'N\x8c\x05start\x94K\x00\x8c\x04stop\x94K\x03\x8c\x04step\x94K\x01u\x86\x94R\x94e]\x94h\x14h\x17K\x00\x85\x94h\x19\x87\x94R\x94(K\x01K\x01K\x03\x86\x94h\x1e\x8c\x02i8\x94K\x00K\x01\x87\x94R\x94(K\x03\x8c\x01<\x94NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00t\x94b\x89C\x18\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x94t\x94ba]\x94h\rh\x0f}\x94(h\x11h\x14h\x17K\x00\x85\x94h\x19\x87\x94R\x94(K\x01K\x01\x85\x94h!\x89]\x94h%at\x94bh'Nu\x86\x94R\x94a}\x94\x8c\x060.14.1\x94}\x94(\x8c\x04axes\x94h\n\x8c\x06blocks\x94]\x94}\x94(\x8c\x06values\x94h6\x8c\x08mgr_locs\x94\x8c\x08builtins\x94\x8c\x05slice\x94\x93\x94K\x00K\x01K\x01\x87\x94R\x94uaust\x94b\x8c\x04_typ\x94\x8c\tdataframe\x94\x8c\t_metadata\x94]\x94\x8c\x05attrs\x94}\x94ub."])

对于NumPy来说,Dask会使用pickle,但也需要更多的信息来使事情变得快速和减少副本。 幸运的是,Dask可以 自定义序列器 对于特定的数据类型

In [8]: serialize(np.arange(5))
Out[8]:
({'dtype': (0, '<i8'),
  'shape': (5,),
  'strides': (8,),
  'lengths': [40],
  'type': 'numpy.ndarray',
  'type-serialized': b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00\x8c\x05numpy\x94\x8c\x07ndarray\x94\x93\x94.',
  'serializer': 'dask'},
 [<memory at 0x118d942c0>])

我建议你读完 https:/distributed.dask.orgenlatestserialization.html。 关于Dask序列化的具体内容,或通过以下方式查看 https:/github.comdaskdistributedblobmasterdistributedprotocol。 关于如何实现序列化的代码细节

© www.soinside.com 2019 - 2024. All rights reserved.