Pickling a Betterproto message
Hi,
I have found an issue when I wanted to use this library in combination with multiprocessing/pickling. I am using version 2.0.0b5
When the resulting message is send back by one of my running processes with the map function it changes empty values and also when I only run pickle they are changed.
The message:
syntax = "proto3";
message TestMessage {
string value = 1;
}
To code:
test_message = TestMessage()
print(test_message)
pickled = pickle.dumps(test_message)
unpickled = pickle.loads(pickled)
print(pickle.loads(pickled))
print(unpickled.value)
print(type(unpickled.value))
The output:
TestMessage()
TestMessage(value=<object object at 0x7febf0791fe0>)
<object object at 0x7fd9ef242ff0>
<class 'object'>
So it seems it adds an object instead of an empty value.
Thanks for the bug report, if this is critically important you can just pass the bytes for the message around until someone implements the required methods for pick to not do this
Yes that indeed works but it saves a lot of processing time when I could use pickle. Because when I test pickle vs TestMessage().parse() for a bit more complicated messages it seems to be almost 10 times faster.
I think to fix this we need to implement the dunder methods __getstate__ and __setstate__ on betterproto.Message?
See the following article section: https://docs.python.org/3/library/pickle.html#handling-stateful-objects