~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to docs/source/serialization.rst

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. PyZMQ serialization doc, by Min Ragan-Kelley, 2011
 
2
 
 
3
.. _serialization:
 
4
 
 
5
Serializing messages with PyZMQ
 
6
===============================
 
7
 
 
8
When sending messages over a network, you often need to marshall your data into bytes.
 
9
 
 
10
 
 
11
Builtin serialization
 
12
---------------------
 
13
 
 
14
PyZMQ is primarily bindings for libzmq, but we do provide three builtin serialization
 
15
methods for convenience, to help Python developers learn libzmq. Python has two primary
 
16
packages for serializing objects: :py:mod:`json` and :py:mod:`pickle`, so we provide
 
17
simple convenience methods for sending and receiving objects serialized with these
 
18
modules. A socket has the methods :meth:`~.Socket.send_json` and
 
19
:meth:`~.Socket.send_pyobj`, which correspond to sending an object over the wire after
 
20
serializing with json and pickle respectively, and any object sent via those
 
21
methods can be reconstructed with the :meth:`~.Socket.recv_json` and
 
22
:meth:`~.Socket.recv_pyobj` methods.
 
23
 
 
24
 
 
25
These methods designed for convenience, not for performance, so developers who do want 
 
26
to emphasize performance should use their own serialized send/recv methods.
 
27
 
 
28
Using your own serialization
 
29
----------------------------
 
30
 
 
31
In general, you will want to provide your own serialization that is optimized for your
 
32
application or library availability.  This may include using your own preferred
 
33
serialization ([msgpack]_, [protobuf]_), or adding compression via [zlib]_ in the standard
 
34
library, or the super fast [blosc]_ library.
 
35
 
 
36
There are two simple models for implementing your own serialization: write a function
 
37
that takes the socket as an argument, or subclass Socket for use in your own apps.
 
38
 
 
39
For instance, pickles can often be reduced substantially in size by compressing the data.
 
40
The following will send *compressed* pickles over the wire:
 
41
 
 
42
.. sourcecode:: python
 
43
 
 
44
    import zlib, cPickle as pickle
 
45
 
 
46
    def send_zipped_pickle(socket, obj, flags=0, protocol=-1):
 
47
        """pickle an object, and zip the pickle before sending it"""
 
48
        p = pickle.dumps(obj, protocol)
 
49
        z = zlib.compress(p)
 
50
        return socket.send(z, flags=flags)
 
51
 
 
52
    def recv_zipped_pickle(socket, flags=0, protocol=-1):
 
53
        """inverse of send_zipped_pickle"""
 
54
        z = socket.recv(flags)
 
55
        p = zlib.uncompress(z)
 
56
        return pickle.loads(p)
 
57
 
 
58
A common data structure in Python is the numpy array.  PyZMQ supports sending
 
59
numpy arrays without copying any data, since they provide the Python buffer interface.
 
60
However just the buffer is not enough information to reconstruct the array on the
 
61
receiving side.  Here is an example of a send/recv that allow non-copying
 
62
sends/recvs of numpy arrays including the dtype/shape data necessary for reconstructing
 
63
the array.
 
64
 
 
65
.. sourcecode:: python
 
66
 
 
67
    import numpy
 
68
 
 
69
    def send_array(socket, A, flags=0, copy=True, track=False):
 
70
        """send a numpy array with metadata"""
 
71
        md = dict(
 
72
            dtype = str(A.dtype),
 
73
            shape = A.shape,
 
74
        )
 
75
        socket.send_json(md, flags|zmq.SNDMORE)
 
76
        return socket.send(A, flags, copy=copy, track=track)
 
77
 
 
78
    def recv_array(socket, flags=0, copy=True, track=False):
 
79
        """recv a numpy array"""
 
80
        md = socket.recv_json(flags=flags)
 
81
        msg = socket.recv(flags=flags, copy=copy, track=track)
 
82
        buf = buffer(msg)
 
83
        A = numpy.frombuffer(buf, dtype=md['dtype'])
 
84
        return A.reshape(md['shape'])
 
85
 
 
86
 
 
87
.. [msgpack] Message Pack serialization library http://msgpack.org
 
88
.. [protobuf] Google Protocol Buffers http://code.google.com/p/protobuf
 
89
.. [zlib] Python stdlib module for zip compression: :py:mod:`zlib`
 
90
.. [blosc] Blosc: A blocking, shuffling and loss-less (and crazy-fast) compression library http://blosc.pytables.org/trac