8000 README documentation of advanced Unpacker features · xunzhang/msgpack-python@de3724c · GitHub
[go: up one dir, main page]

Skip to content

Commit de3724c

Browse files
committed
README documentation of advanced Unpacker features
1 parent c567ad1 commit de3724c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ Also possible to pack/unpack user's data types. Here is an example for
9494
``object_pairs_hook`` callback may instead be used to receive a list of
9595
key-value pairs.
9696

97+
98+
advanced unpacking control
99+
^^^^^^^^^^^^^^^^^^^^^^^^^^
100+
101+
As an alternative to iteration, ``Unpacker`` objects provide ``unpack``,
102+
``skip``, ``read_array_header`` and ``read_map_header`` methods. The former two
103+
read an entire message from the stream, respectively deserialising and returning
104+
the result, or ignoring it. The latter two methods return the number of elements
105+
in the upcoming container, so that each element in an array, or key-value pair
106+
in a map, can be unpacked or skipped individually.
107+
108+
Each of these methods may optionally write the packed data it reads to a
109+
callback function:
110+
111+
::
112+
113+
from io import BytesIO
114+
115+
def distribute(unpacker, get_worker):
116+
nelems = unpacker.read_map_header()
117+
for i in range(nelems):
118+
# Select a worker for the given key
119+
key = unpacker.unpack()
120+
worker = get_worker(key)
121+
122+
# Send the value as a packed message to worker
123+
bytestream = BytesIO()
124+
unpacker.skip(bytestream.write)
125+
worker.send(bytestream.getvalue())
126+
97127
INSTALL
98128
---------
99129
You can use ``pip`` or ``easy_install`` to install msgpack::

0 commit comments

Comments
 (0)
0