Install

PyPy

Windows

When you can’t use binary distribution, you need to install Visual Studio
or Windows SDK on Windows. (NOTE: Visual C++ Express 2010 doesn’t support
amd64. Windows SDK is recommanded way to build amd64 msgpack without any fee.)

Without extension, using pure python implementation on CPython runs slowly.

Advanced unpacking control

As an alternative to iteration, Unpacker objects provide unpack,
skip, read_array_header and read_map_header methods. The former two
read an entire message from the stream, respectively deserialising and returning
the result, or ignoring it. The latter two methods return the number of elements
in the upcoming container, so that each element in an array, or key-value pair
in a map, can be unpacked or skipped individually.

Each of these methods may optionally write the packed data it reads to a
callback function:

fromioimportBytesIOdefdistribute(unpacker,get_worker):nelems=unpacker.read_map_header()foriinrange(nelems):# Select a worker for the given keykey=unpacker.unpack()worker=get_worker(key)# Send the value as a packed message to workerbytestream=BytesIO()unpacker.skip(bytestream.write)worker.send(bytestream.getvalue())

Note about performance

GC

CPython’s GC starts when growing allocated object.
This means unpacking may cause useless GC.
You can use gc.disable() when unpacking large message.

use_list option

List is the default sequence type of Python.
But tuple is lighter than list.
You can use use_list=False while unpacking when performance is important.

Python’s dict can’t use list as key and MessagePack allows array for key of mapping.
use_list=False allows unpacking such message.
Another way to unpacking such object is using object_pairs_hook.