@@ -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
9595key-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+
97127INSTALL
98128---------
99129You can use ``pip `` or ``easy_install `` to install msgpack::
0 commit comments