Python中泡菜的常见用例

Python中泡菜的常见用例

本文介绍了Python中泡菜的常见用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 pickle 文档,但我没有了解泡菜在哪里有用.

I've looked at the pickle documentation, but I don't understand where pickle is useful.

泡菜有哪些常见用例?

推荐答案

我遇到的一些用法:

1)将程序的状态数据保存到磁盘上,以便它在重新启动时可以在中断的地方继续工作(持久性)

1) saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)

2)在多核或分布式系统中通过TCP连接发送python数据(编组)

2) sending python data over a TCP connection in a multi-core or distributed system (marshalling)

3)将python对象存储在数据库中

3) storing python objects in a database

4)将任意python对象转换为字符串,以便可以将其用作字典键(例如,用于缓存和备忘录).

4) converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).

最后一个存在一些问题-两个相同的对象可以被腌制并导致不同的字符串-甚至相同的对象两次被腌制也可以具有不同的表示形式.这是因为泡菜可以包含参考计数信息.

There are some issues with the last one - two identical objects can be pickled and result in different strings - or even the same object pickled twice can have different representations. This is because the pickle can include reference count information.

为了强调@lunaryorn的评论-切勿从不可信的来源中提取字符串,因为精心制作的pickle可以在系统上执行任意代码.例如,请参见 https://blog.nelhage.com/2011/03/exploiting-pickle /

To emphasise @lunaryorn's comment - you should never unpickle a string from an untrusted source, since a carefully crafted pickle could execute arbitrary code on your system. For example see https://blog.nelhage.com/2011/03/exploiting-pickle/

这篇关于Python中泡菜的常见用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:31