Google Protobuf simple tutorial

What is Protobuf

Protobuf actually Json or a similar data transmission format and XML specifications for use for communication between different applications or processes. The information transmitted by the communication message is packed Protobuf defined data structure, and then compiled into a binary code stream and then transmitted or stored.

The advantage Protobuf

In comparison, Protobuf has the following advantages:

  • Simple enough
  • After a sequence of small size: the size of the message need only XML 1/10 ~ 1/3
  • Parsing speed: 20 ~ 100 parses times faster than XML
  • Multi-language support
  • Better compatibility, a principle is to be able to support a good Protobuf downward or upward-compatible design

How to install and use Protobuf

installation

Python using simple installation then follows

pip install protobuf    # 安装protobuf库
sudo apt-get install protobuf-compiler  # 安装protobuf编译器

If you compile and install your own, then you can refer to git installation guidance, though not explicit enough :)

use

Protobuf using the following steps:

  1. Definition Message
  2. And storing message transmission initialization message
  3. Read and parse the message

Below is a practical example of how to use Protobuf, to demonstrate actual directory structure of the project:

.
├── my
│   ├── helloworld_pb2.py
│   ├── helloworld_pb2.pyc
│   └── __init__.py
├── mybuffer.io
├── my.helloworld.proto
├── reader.py
└── writer.py

Definition Message

Protobuf message structure is called by the Protocol Buffer Language language to define and describe, in fact Protocol Buffer Language in two versions, version 2 and version 3, used by default without declaration of version 2, below with version 2 to give chestnuts, suppose we define a file named my.helloworld.proto, as follows:

package my;
message helloworld
{
    required int32 id = 1; required string str = 2; optional int32 wow = 3; } 

Then we need to be compiled using protoc

protoc -I=./ --python_out=./ ./my.helloworld.proto
  • -I: source path is set
  • --python_out: setting for outputting the result of the compilation, if other languages ​​use the corresponding language option
  • The last parameter is the file you want to compile proto

Has now defined the data structure of the message, then look at how to use

Initialization message transmission and storage

We initialized by writer.py messages and stored as a file, as follows:

from my.helloworld_pb2 import helloworld

def main(): hw = helloworld() hw.id = 123 hw.str = "eric" print hw with open("mybuffer.io", "wb") as f: f.write(hw.SerializeToString()) if __name__ == "__main__": main() 

After performing writer.py will be serialized result mybuffer.io stored in a file, and then look at how to read

Read news and analysis

We read and parse through the message reader.py, code is as follows:

from my.helloworld_pb2 import helloworld


def main(): hw = helloworld() with open("mybuffer.io", "rb") as f: hw.ParseFromString(f.read()) print hw.id print hw.str if __name__ == "__main__": main() 

Reference:



Author: geekpy
link: https: //www.jianshu.com/p/b723053a86a6
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/ExMan/p/12111221.html