简体   繁体   English

您如何为具有多个 python 包的项目定义/组织 proto 文件?

[英]How do you define/organize proto files for a project with multiple python packages?

How do I define and organize gRPC proto files for a project with two (or more) python packages?如何为具有两个(或更多)python 包的项目定义和组织 gRPC proto 文件?

Suppose I have the following monorepo structure:假设我有以下 monorepo 结构:

my_project/  
 |
 |-my_client_pkg/
 |  |_Dockerfile
 |  |_setup.py
 |  |_my_client/
 |     |___init__.py
 |     |_client.py
 |
  -my_server_pkg/
    |_Dockerfile
    |_setup.py
    |_my_server/
       |___init__.py
       |_server.py

I want to use gRPC to connect these two services together.我想使用 gRPC 将这两个服务连接在一起。 I can add a protos/ directory, with the appropriate child directories like so:我可以添加一个protos/目录,以及适当的子目录,如下所示:

my_project/  
 |
 |-protos/
 |  |_my_client_pkg/
 |  |  |_my_client.proto
 |  |
 |  |_my_server_pkg/
 |     |_my_server.proto
 |
 |-my_client_pkg/
 |  |_Dockerfile
 |  |_setup.py
 |  |_my_client/
 |     |___init__.py
 |     |_client.py
 |
  -my_server_pkg/
    |_Dockerfile
    |_setup.py
    |_my_server/
       |___init__.py
       |_server.py

and this works: I can generate the .py files correctly, with the correct import statements.这有效:我可以使用正确的导入语句正确生成 .py 文件。 The problem with this approach is that I must duplicate everything from my_client.proto into my_server.proto .这种方法的问题是我必须将my_client.proto所有内容复制到my_server.proto If I need to make a change, it must be in both files, something is obviously error prone.如果我需要进行更改,它必须在两个文件中,显然很容易出错。 It really seems to defeat the purpose of protobufs.这似乎真的违背了 protobufs 的目的。

How should I be organizing my proto files?我应该如何组织我的 proto 文件? Is it possible to define the proto in one place, and then use the proto import statement to seamlessly duplicate the definitions?是否可以在一个地方定义proto,然后使用proto import语句无缝复制定义? Should I be doing something completely different?我应该做一些完全不同的事情吗?

Edit: Here is an example proto file:编辑:这是一个示例 proto 文件:

syntax = "proto3";

message AbcRequest {
  string utterance = 1;
}

message AbcResponse {
  string value = 2;
}

service Abc {
  rpc most_similar(AbcRequest) returns (AbcResponse) {}
}

I did try defining the proto in a single place, and then importing it:我确实尝试在一个地方定义原型,然后导入它:

my_project/  
 |
 |-protos/
 |  |-base.proto
 |  |_my_client_pkg/
 |  |  |_my_client.proto
 |  |
 |  |_my_server_pkg/
 |     |_my_server.proto

Where my_client.proto and my_server.proto both just have: my_client.proto 和 my_server.proto 都只有:

syntax = "proto3";
import "protos/base.proto";

However, when I generated the python code, the *_grpc.py file had nothing in it.但是,当我生成 python 代码时, *_grpc.py 文件中没有任何内容。 Should I be doing something else for the importing to work like this?我应该为导入工作做其他事情吗?

protobuf import statements allow you to access definitions defined in other files, but will only generated code for messages/services defined in that file. protobuf import语句允许您访问其他文件中定义的定义,但只会为该文件中定义的消息/服务生成代码。
syntax = "proto3"; import "protos/base.proto";

Will generate empty files because there are no service/messages defined in this file.将生成空文件,因为此文件中没有定义服务/消息。

The code for the file you imported will live in base_pb2_grpc .您导入的文件的代码将位于base_pb2_grpc

I would recommend having a single my_service.proto file with all your definitions, and just referencing the generated _pb2_grpc.py file from both your my_client and my_server package.我建议有一个包含所有定义的my_service.proto文件,并且只引用 my_client 和 my_server 包中生成的_pb2_grpc.py文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM