8000 docs: clarify that proto plus messages are not pickleable (#260) · renovate-bot/proto-plus-python@6e691dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 6e691dc

Browse files
authored
docs: clarify that proto plus messages are not pickleable (googleapis#260)
Clarify that proto-plus messages are not pickleable, describe the alternative, and give a multiprocessing example. Note: in the future, we _may_ enable pickling proto-plus messages through custom extensions as described in https://docs.python.org/3/library/pickle.html#pickling-class-instances This is _not_ guaranteed. For the foreseeable future, the preferred method of serializing and deserializing proto-plus messages is Message.serialize and Message.deserialize
1 parent 37c7550 commit 6e691dc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

docs/messages.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,32 @@ already allows construction from mapping types.
243243
song_dict = Song.to_dict(song)
244244
245245
new_song = Song(song_dict)
246+
247+
.. note::
248+
249+
Protobuf messages **CANNOT** be safely pickled or unpickled. This has serious consequences for programs that use multiprocessing or write messages to files.
250+
The preferred mechanism for serializing proto messages is :meth:`~.Message.serialize`.
251+
252+
Multiprocessing example:
253+
254+
.. code-block:: python
255+
256+
import proto
257+
from multiprocessing import Pool
258+
259+
class Composer(proto.Message):
260+
name = proto.Field(proto.STRING, number=1)
261+
genre = proto.Field(proto.STRING, number=2)
262+
263+
composers = [Composer(name=n) for n in ["Bach", "Mozart", "Brahms", "Strauss"]]
264+
265+
with multiprocessing.Pool(2) as p:
266+
def add_genre(comp_bytes):
267+
composer = Composer.deserialize(comp_bytes)
268+
composer.genre = "classical"
269+
return Composer.serialize(composer)
270+
271+
updated_composers = [
272+
Composer.deserialize(comp_bytes)
273+
for comp_bytes in p.map(add_genre, (Composer.serialize(comp) for comp in composers))
274+
]

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def unitcpp(session):
5959
return unit(session, proto="cpp")
6060

6161

62-
@nox.session(python="3.7")
62+
@nox.session(python="3.9")
6363
def docs(session):
6464
"""Build the docs."""
6565

0 commit comments

Comments
 (0)
0