Hi Phil,

If you are not doing so already, I would highly recommend using the C++ 
implementation for Python (instead of the pure-Python one). It will make 
ParseFromString() in Python much, much faster, which might give you the 
speed boost you need.

There are possible ways of doing more clever things, but they would require 
you to use the C++ implementation anyway, and they would make you more 
dependent on the internals of the Python/C++ library.

On Monday, January 4, 2016 at 5:35:27 PM UTC-8, Philipp Schrader wrote:
>
> Hi all,
>
>
> I'm wondering if anyone knows of a better way to convert a C++ protobuf 
> into a Python protobuf.
>
> I'm writing a Python module in C++ that wraps some of our protobuf-related 
> functionality such as reading our logs.
> Right now I'm serializing the C++ protobuf and then de-serializing it in 
> Python.
> I'd love to avoid this performance cost.
>
> Here's roughly the code I'm using:
>
>
> // Python setup code
> const char pb_name[] = "foo.Message1";
> PyObject* database_module = 
> PyImport_ImportModule("google.protobuf.symbol_database");
> PyObject* database = PyObject_CallMethod(database_module, "Default", nullptr);
> PyObject *msg_class = PyObject_CallMethod(database, "GetSymbol", "s", 
> pb_name);
> ...
>
> PyObject* ConvertToPythonPB(const ::google::protobuf::Message &msg) {
>   // Serialize into string
>   auto serialized_msg = msg.SerializeAsString();
>   // Create a new message instance
>   PyObject *py_msg = PyObject_CallObject(msg_class, nullptr);
>   // Deserialize into the Pyhton object
>   PyObject *result = PyObject_CallMethod(py_msg, "ParseFromString", "y#",
>                                          serialized_msg.data(), 
> serialized_msg.size());
>   ...
>   return result;
> }
>
> I've looked into using the cpp protobuf implementation for Python, but I 
> haven't had any luck calling that C++ code from my module.
>
> Is there documentation that I'm overlooking? I can't find anything on how 
> to do this more easily.
>
>
> Thanks,
> Phil
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to