gpt4 book ai didi

protocol-buffers - 如何让 protoc 编译一个 proto 及其依赖项?

转载 作者:行者123 更新时间:2023-12-05 05:51:55 29 4
gpt4 key购买 nike

我需要修复一个 grpc 服务,所以我想了解编译它们的逻辑。

在下面的示例中,我不明白为什么 protoc 不编译 address.proto,因为它是由 person.proto 导入的。

没有构建错误,所以我认为这不是导入/命名问题。不幸的是,只生成了一个person_pb2.py...

// file: address.proto

syntax="proto3";

message Address {
string city = 1;
string road = 3;
int32 roadNumber = 4;
}
// file: person.proto

syntax="proto3";

import "address.proto";

message Person {
string name = 1;
Address home = 3;
Address work = 4;
}

构建命令:

python -m grpc_tools.protoc --proto_path ../protos --python_out=. person.proto

最佳答案

这是一个合理的问题。

答案是你只是要求 protoc 来编译 person.proto(而且它是按字面意思)。但是,生成的代码不会运行,因为 person_pb2.py 依赖于 address_pb2:

import address_pb2 as address__pb2

您需要为您的代码所需的所有 Protocol Buffer 类型提供 Python 源代码。

python3 \
-m grpc_tools.protoc \
--proto_path=../protos \
--python_out=. \
person.proto address.proto

这个问题(正在解决)的一个很好的例子是谷歌所谓的 Well-Known Types (WKTs)

您可以包括例如google.protobuf.TimestampPerson 中添加 import "google/protobuf/timestamp.proto":

syntax="proto3";

import "google/protobuf/timestamp.proto";
import "address.proto";

message Person {
string name = 1;
Address home = 3;
Address work = 4;
google.protobuf.Timestamp timestamp = 5;
}

并且您可以protoc无需更改并且您的代码将工作。

这是因为 Google 将 WKTs 生成的 Python 代码与 grpcio-tool 捆绑在一起。与任何其他来源一样,您确实需要导入 Google 提供的 Python 来源才能使用代码。

为此,你必须

main.py:

import address_pb2
import person_pb2

from google.protobuf.timestamp_pb2 import Timestamp

那么,这些文件在哪里?

以我为例:

lib/python3.8/site-packages/grpc_tools/_proto/google/protobuf/timestamp.proto

和:

lib/python3.8/site-packages/google/protobuf/timestamp_pb2.py

NOTE
It's convenient that Google bundles the generated sources but this is not required and it creates two potential problems.

  1. timestamp.proto is the source-of-truth and we must assume|trust that google/protobuf/timestamp_pb2.py was generated from it.
  2. If we're the proto developer, we're expected to produce sources for all languages that our proto's developers may use every time we update the protos.

For these reasons, generally (!) developers provide only the protos and don't include generated sources and assume that you're able to plug the proto into protoc and generated the perfect facsimile as code yourself.

关于protocol-buffers - 如何让 protoc 编译一个 proto 及其依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70295689/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com