有没有办法让 protoc__init__.py在它用来生成 .py 文件的目录中生成空文件,以便它们可以用作模块?
python python-module python-2.x python-import protobuf-python
我收到这个错误
Can't pickle <class 'google.protobuf.pyext._message.CMessage'>: it's not found as google.protobuf.pyext._message.CMessage
当我尝试在 PySpark 中创建 UDF 时。显然,它使用 CloudPickle 来序列化命令,但是,我知道 protobuf 消息包含C++实现,这意味着它不能被腌制。
我试图找到一种方法来覆盖CloudPickleSerializer,但是,我找不到方法。
这是我的示例代码:
from MyProject.Proto import MyProtoMessage
from google.protobuf.json_format import MessageToJson
import pyspark.sql.functions as F
def proto_deserialize(body):
msg = MyProtoMessage()
msg.ParseFromString(body)
return MessageToJson(msg)
from_proto = F.udf(lambda s: proto_deserialize(s))
base.withColumn("content", from_proto(F.col("body")))
Run Code Online (Sandbox Code Playgroud)
提前致谢。
protobuf 是否支持 python 的相对导入?
我未能成功创建支持此功能的 protobuf 构建脚本。从我的 .proto 文件生成 python 类时,如果我从与创建生成的 .py 文件的文件夹相同的文件夹中启动 python,我只能导入 python 模块。
我已经构建了以下 MVP。理想情况下,我想要一个结构,其中生成的 python 代码放置在一个单独的文件夹(例如./generated)中,然后我可以移动到其他项目中。我已经发布了我已经开始工作的方法,但我希望更有经验的人能够为我指出更好的解决方案。
基本信息:
文件夹结构:
.
|--- schemas
|---- main_class.proto
|---- sub
|----sub_class.proto
|--- generated
Run Code Online (Sandbox Code Playgroud)
syntax = "proto3";
import public "sub/sub_class.proto";
message MainClass {
repeated SubClass subclass = 1;
}
Run Code Online (Sandbox Code Playgroud)
syntax = "proto3";
message LogMessage {
enum Status {
STATUS_ERROR = 0;
STATUS_OK = 1;
}
Status status = 1;
string timestamp = …Run Code Online (Sandbox Code Playgroud) 我试图看看是否可以在单个 GRPC 服务器上运行同一服务的不同实例,但看起来我无法这样做。所以我想知道我的测试是否做错了什么,或者根本不可能。
我的测试基于grpc 存储库中的示例/python/多路复用:
服务:
class _GreeterServicer(helloworld_pb2_grpc.GreeterServicer):
def __init__(self, greeter):
self.greeter = greeter
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(
message='Hello, {}! This is {}!'.format(request.name, self.greeter))
Run Code Online (Sandbox Code Playgroud)
服务器:
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("John"),
server)
helloworld_pb2_grpc.add_GreeterServicer_to_server(_GreeterServicer("Jim"),
server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
Run Code Online (Sandbox Code Playgroud)
客户:
def run():
for _ in range(10):
with grpc.insecure_channel('localhost:50051') as channel:
greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
greeter_response = greeter_stub.SayHello(
helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + greeter_response.message)
Run Code Online (Sandbox Code Playgroud)
由于我为每次迭代打开一个新通道,因此我期望得到包含“你好,你!这是吉姆!”的输出。和“你好!我是约翰!”,但我只得到:
Greeter client received: Hello, you! This is John!
Greeter client received: Hello, you! This is …Run Code Online (Sandbox Code Playgroud) 假设我有一条消息定义为test.proto:
message TestMessage {\n int64 id = 1;\n string title = 2;\n string subtitle = 3;\n string description = 4;\n}\nRun Code Online (Sandbox Code Playgroud)\n我使用 protoc 将其转换为 Python,如下所示:
\nprotoc --python_out=. test.proto
时间PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python:
from test_pb2 import TestMessage\n\n%%timeit\ntm = TestMessage()\ntm.id = 1\ntm.title = 'test title'\ntm.subtitle = 'test subtitle'\ntm.description = 'this is a test description'\nRun Code Online (Sandbox Code Playgroud)\n6.75 \xc2\xb5s \xc2\xb1 152 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)
时间PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=cpp:
是否可以使用 protoc 从生成的 pb2.py 中获取 proto 文件?gRPC 是否可以进行相同的逆向工程?
我目前正在尝试从 proto 文件生成 python 代码。
我的原型文件如下所示:
syntax = "proto3";
package display;
message Hello {
uint32 version = 1;
uint32 value = 2;
int32 id = 3;
}
Run Code Online (Sandbox Code Playgroud)
我使用此protoc命令生成 python 代码:
protoc -I="." --python_out="." test.proto
Run Code Online (Sandbox Code Playgroud)
这是生成的 python 文件:
syntax = "proto3";
package display;
message Hello {
uint32 version = 1;
uint32 value = 2;
int32 id = 3;
}
Run Code Online (Sandbox Code Playgroud)
它看起来与本页上的 Google 文档完全不同。
为什么没有生成元类?
我正在使用 Python 3.9 以及最新版本的 protobuf 包和最新版本的 protoc。
我正在处理 protobuf 协议,并且遇到需要解码未知字段和类型的消息。我知道protoc --decode_raw这方面做得很好(可惜不够精确,但足够好)。
我正在考虑protoc --decode-raw在 shell 中运行并让 Python 读取其内容,将其解析为字典,但我认为这是实现的最后手段。
是否有实现相同功能的 Pythonic 方法?
I'm new to protobuf, so I don't know how to frame the question correctly.
Anyways, I'm using this Model Config proto file. I converted it into python using this command protoc -I=. --python_out=. ./model_server_config.proto from Protocol Buffer page. Now I have some python files which I can import and work on. My objective is to create a file (for running the TensorFlow model server with multiple models) which should look like the following:
model_config_list: {
config: {
name: "name1", …Run Code Online (Sandbox Code Playgroud) python ×8
grpc ×2
protoc ×2
databricks ×1
grpc-python ×1
proto ×1
pyspark ×1
python-2.x ×1
python-3.x ×1
tensorflow ×1