序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。

在scrapy_redis中,一个Request对象先经过DupeFilter去重,然后递交给scheduler调度储存在Redis中,这就面临一个问题,Request是一个对象,Redis不能存储该对象,这时就需要将request序列化储存。

scrapy中序列化模块如下:

from scrapy_redis import picklecompat

"""A pickle wrapper module with protocol=-1 by default."""try:import cPickle as pickle  # PY2
except ImportError:import pickledef loads(s):return pickle.loads(s)def dumps(obj):return pickle.dumps(obj, protocol=-1)

当然python3直接使用pickle模块, 已经没有cPickle,该模块最为重要的两个方法,序列化与反序列化如上,通过序列化后的对象我们可以存储在数据库、文本等文件中,并快速恢复。

同时模式设计中的备忘录模式通过这种方式达到最佳效果《python设计模式(十九):备忘录模式》;可序列化的对象和数据类型如下:

  • None, True,False
  • 整数,长整数,浮点数,复数
  • 普通字符串和Unicode字符串
  • 元组、列表、集合和字典,只包含可选择的对象。
  • 在模块顶层定义的函数
  • 在模块顶层定义的内置函数
  • 在模块的顶层定义的类。
  • 这些类的实例

尝试对不可序列化对象进行操作,将引发PicklingError异常;发生这种情况时,可能已经将未指定的字节数写入基础文件。尝试选择高度递归的数据结构可能会超过最大递归深度,RuntimeError在这种情况下会被提起。

模块API

pickle.dump(obj, file[, protocol])

  • Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
    If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.Changed in version 2.3: Introduced the protocol parameter.file must have a write() method that accepts a single string argument. It can thus be a file object opened for writing, a StringIO object, or any other custom object that meets this interface.
  • pickle.load(file)
  • Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy. This is equivalent to Unpickler(file).load().file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.
    This function automatically determines whether the data stream was written in binary mode or not.
  • pickle.dumps(obj[, protocol])
  • Return the pickled representation of the object as a string, instead of writing it to a file.
    If the protocol parameter is omitted, protocol 0 is used. If protocol is specified as a negative value or HIGHEST_PROTOCOL, the highest protocol version will be used.Changed in version 2.3: The protocol parameter was added.
  • pickle.loads(string)
  • Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.

至于应用场景,比较常见的有如下几种:

程序重启时恢复上次的状态、会话存储、对象的网络传输。

redis序列化_scrapy_redis中序列化源码及其在程序设计中的应用相关推荐

  1. 20220910最新版Redis7源码编译及windows中安装

    20220910最新版Redis7源码编译及windows中安装 文章目录 20220910最新版Redis7源码编译及windows中安装 1.Cygwin安装 1 Cygwin介绍 ...is i ...

  2. 如何将spring源码作为导入eclipse中,变成一个普通的项目(git、github)

    引子: 怎么查看spring-framework的源码?是不是用压缩软件解压jar包,然后用编辑软件看?高端一点的,是在eclipse上面,按住Ctrl键跳转着看?这里我给大家介绍更加高端一点的方法. ...

  3. JDK源码解析 —— IO流中的包装类使用到了装饰者模式

    JDK源码解析 IO流中的包装类使用到了装饰者模式. BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter ...

  4. java 自定义arraylist_Java 中模仿源码自定义ArrayList

    Java 中模仿源码自定义ArrayList 最近看了下ArrayList的源码,抽空根据ArrayList的底层结构写了一个功能简单无泛型的自定义ArrayLsit,帮助自己更好理解ArrayLis ...

  5. 从源码角度解析Android中APK安装过程

    从源码角度解析Android中APK的安装过程 1. Android中APK简介 Android应用Apk的安装有如下四种方式: 1.1 系统应用安装 没有安装界面,在开机时自动完成 1.2 网络下载 ...

  6. linux ssh rpm包,RHEL6(CentOS6)中使用源码包编译生成RPM的基本方法:升级OpenSSH篇

    RHEL6(CentOS6)中使用源码包编译生成RPM的基本方法:升级OpenSSH篇 具体过程请见代码~ # cp openssh-7.1p1.tar.gz /root/rpmbuild/SOURC ...

  7. c++调用mysql存储过程_C++中ADO调用MySQL存储过程失败,诡异的语法异常,求解中,附源码...

    C++中ADO调用mysql存储过程失败,诡异的语法错误,求解中,附源码 不管怎么调整,死活都出现下面的错误 C++ADO代码我实在找不到什么错误了,难道是MySQL有什么问题,或者有其他需要注意的地 ...

  8. Openfire4源码部署到eclipse中并编译

    Openfire4源码部署到eclipse中并编译 概述 Openfire是众所周知的基于xmpp协议的IM开源服务,所有操作,配置,监控,调试等以B/S方式进行展示,非常的方便管理员进行管理.它的强 ...

  9. 如何在eclipse中查看源码

    在eclipse中查看源码 作为一个java开发的程序员,特别是初学者的java程序员.有时候我们很有必要查看java本身的一些工具类的源码,但是在这之前需要进行一项设置才能顺利的查看得到我们JKD自 ...

最新文章

  1. 中文扩增子分析视频教程推荐
  2. mysql slave 配置_【mysql5.6】 数据库主从(Master/Slave)配置记录
  3. centos安装Flash插件
  4. MATLAB实战系列(二十九)-头脑风暴优化(BSO)算法求解旅行商问题(TSP)-交叉算子
  5. Java中对象的深克隆和浅克隆
  6. 95. Unique Binary Search Trees II
  7. HDU - 4856 Tunnels(哈密顿路径+状压dp)
  8. python字符串的特点_python小白之路(特性语法三之字符串)
  9. Java Date Time 教程-时间测量
  10. python编辑编程器_用Python制作编辑器
  11. 在Mac下配置Macaca环境
  12. 蓝桥杯 ADV-166 算法提高 聪明的美食家 java版
  13. 40岁,将站到哪里(转)
  14. Linux基础-获取命令帮助与命令的查找(1)
  15. UnityShader[1]光照模型
  16. JAVA对接圆通API
  17. execute()方法
  18. peewee-async使用描述
  19. 大话西游之Office应用实例系列! 16
  20. bootStrap 教程 文档

热门文章

  1. JS----JavaScript数组方法及总结
  2. Vue面试题 70道题目及答案
  3. 中国网建SMS短信接口调用(java发送短信)
  4. JavaScript数组结构与算法——数组详解(中)
  5. ionic4安卓真机调试
  6. CaffeMFC:caffe.pb.h(2525): error C2059: syntax error : 'constant'
  7. Angular之组件的创建
  8. Statement对象
  9. LAMP-----3、配置apache实现与php的整合
  10. 项目管理系列--谷歌的code review