2010-11-11 15:00:44 +00:00
|
|
|
.. _guide-serialization:
|
|
|
|
|
|
|
|
===============
|
|
|
|
Serialization
|
|
|
|
===============
|
|
|
|
|
|
|
|
.. _serializers:
|
|
|
|
|
|
|
|
Serializers
|
|
|
|
===========
|
|
|
|
|
|
|
|
By default every message is encoded using `JSON`_, so sending
|
|
|
|
Python data structures like dictionaries and lists works.
|
|
|
|
`YAML`_, `msgpack`_ and Python's built-in `pickle` module is also supported,
|
|
|
|
and if needed you can register any custom serialization scheme you
|
|
|
|
want to use.
|
|
|
|
|
2013-10-04 15:26:17 +00:00
|
|
|
|
|
|
|
By default Kombu will only load JSON messages, so if you want
|
|
|
|
to use other serialization format you must explicitly enable
|
|
|
|
them in your consumer by using the ``accept`` argument:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
Consumer(conn, [queue], accept=['json', 'pickle', 'msgpack'])
|
|
|
|
|
|
|
|
The accept argument can also include MIME-types.
|
|
|
|
|
2010-11-11 15:00:44 +00:00
|
|
|
.. _`JSON`: http://www.json.org/
|
|
|
|
.. _`YAML`: http://yaml.org/
|
2017-08-20 11:31:10 +00:00
|
|
|
.. _`msgpack`: https://msgpack.org/
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
Each option has its advantages and disadvantages.
|
|
|
|
|
2022-04-16 10:57:37 +00:00
|
|
|
`json` -- JSON is supported in many programming languages, is
|
|
|
|
a standard part of Python, and is fairly fast to
|
|
|
|
decode.
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
The primary disadvantage to `JSON` is that it limits you to
|
2011-11-02 02:39:48 +00:00
|
|
|
the following data types: strings, Unicode, floats, boolean,
|
2022-07-17 17:09:40 +00:00
|
|
|
dictionaries, lists, decimals, DjangoPromise, datetimes, dates,
|
|
|
|
time, bytes and UUIDs.
|
|
|
|
|
|
|
|
For dates, datetimes, UUIDs and bytes the serializer will generate
|
|
|
|
a dict that will later instruct the deserializer how to produce
|
|
|
|
the right type.
|
2010-11-11 15:00:44 +00:00
|
|
|
|
2011-11-02 02:39:48 +00:00
|
|
|
Also, binary data will be transferred using Base64 encoding, which
|
2010-11-11 15:00:44 +00:00
|
|
|
will cause the transferred data to be around 34% larger than an
|
2022-07-17 17:09:40 +00:00
|
|
|
encoding which supports native binary types. This will only happen
|
|
|
|
if the bytes object can't be decoded into utf8.
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
However, if your data fits inside the above constraints and
|
|
|
|
you need cross-language support, the default setting of `JSON`
|
|
|
|
is probably your best choice.
|
|
|
|
|
2023-03-02 14:11:03 +00:00
|
|
|
If you need support for custom types, you can write serialize/deserialize
|
|
|
|
functions and register them as follows:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from kombu.utils.json import register_type
|
|
|
|
from django.db.models import Model
|
|
|
|
from django.apps import apps
|
|
|
|
|
|
|
|
# Allow serialization of django models:
|
|
|
|
register_type(
|
|
|
|
Model,
|
|
|
|
"model",
|
|
|
|
lambda o: [o._meta.label, o.pk],
|
|
|
|
lambda o: apps.get_model(o[0]).objects.get(pk=o[1]),
|
|
|
|
)
|
|
|
|
|
2010-11-11 15:00:44 +00:00
|
|
|
`pickle` -- If you have no desire to support any language other than
|
|
|
|
Python, then using the `pickle` encoding will gain you
|
|
|
|
the support of all built-in Python data types (except class instances),
|
|
|
|
smaller messages when sending binary files, and a slight speedup
|
|
|
|
over `JSON` processing.
|
|
|
|
|
2013-10-04 15:26:17 +00:00
|
|
|
.. admonition:: Pickle and Security
|
|
|
|
|
|
|
|
The pickle format is very convenient as it can serialize
|
|
|
|
and deserialize almost any object, but this is also a concern
|
|
|
|
for security.
|
|
|
|
|
|
|
|
Carefully crafted pickle payloads can do almost anything
|
|
|
|
a regular Python program can do, so if you let your consumer
|
|
|
|
automatically decode pickled objects you must make sure
|
|
|
|
to limit access to the broker so that untrusted
|
|
|
|
parties do not have the ability to send messages!
|
|
|
|
|
2022-04-05 23:22:53 +00:00
|
|
|
By default Kombu uses pickle protocol 4, but this can be changed
|
2012-08-04 13:22:21 +00:00
|
|
|
using the :envvar:`PICKLE_PROTOCOL` environment variable or by changing
|
|
|
|
the global :data:`kombu.serialization.pickle_protocol` flag.
|
|
|
|
|
2010-11-11 15:00:44 +00:00
|
|
|
`yaml` -- YAML has many of the same characteristics as `json`,
|
|
|
|
except that it natively supports more data types (including dates,
|
|
|
|
recursive references, etc.)
|
|
|
|
|
|
|
|
However, the Python libraries for YAML are a good bit slower
|
|
|
|
than the libraries for JSON.
|
|
|
|
|
|
|
|
If you need a more expressive set of data types and need to maintain
|
|
|
|
cross-language compatibility, then `YAML` may be a better fit
|
|
|
|
than the above.
|
|
|
|
|
2012-01-20 15:12:00 +00:00
|
|
|
To instruct `Kombu` to use an alternate serialization method,
|
2010-11-11 15:00:44 +00:00
|
|
|
use one of the following options.
|
|
|
|
|
2016-07-30 02:37:05 +00:00
|
|
|
1. Set the serialization option on a per-producer basis:
|
2016-04-07 22:59:26 +00:00
|
|
|
|
|
|
|
.. code-block:: pycon
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
>>> producer = Producer(channel,
|
|
|
|
... exchange=exchange,
|
2016-03-21 22:47:07 +00:00
|
|
|
... serializer='yaml')
|
2010-11-11 15:00:44 +00:00
|
|
|
|
2016-07-30 02:37:05 +00:00
|
|
|
2. Set the serialization option per message:
|
2016-04-07 22:59:26 +00:00
|
|
|
|
|
|
|
.. code-block:: pycon
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
>>> producer.publish(message, routing_key=rkey,
|
2016-03-21 22:47:07 +00:00
|
|
|
... serializer='pickle')
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
Note that a `Consumer` do not need the serialization method specified.
|
|
|
|
They can auto-detect the serialization method as the
|
|
|
|
content-type is sent as a message header.
|
|
|
|
|
|
|
|
.. _sending-raw-data:
|
|
|
|
|
|
|
|
Sending raw data without Serialization
|
|
|
|
======================================
|
|
|
|
|
|
|
|
In some cases, you don't need your message data to be serialized. If you
|
2015-07-01 11:49:34 +00:00
|
|
|
pass in a plain string or Unicode object as your message and a custom `content_type`, then `Kombu` will
|
2010-11-11 15:00:44 +00:00
|
|
|
not waste cycles serializing/deserializing the data.
|
|
|
|
|
2015-07-01 11:49:34 +00:00
|
|
|
You can optionally specify a `content_encoding`
|
2016-04-07 22:59:26 +00:00
|
|
|
for the raw data:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2010-11-11 15:00:44 +00:00
|
|
|
|
2016-03-21 22:47:07 +00:00
|
|
|
>>> with open('~/my_picture.jpg', 'rb') as fh:
|
2011-11-02 02:39:48 +00:00
|
|
|
... producer.publish(fh.read(),
|
2016-03-21 22:47:07 +00:00
|
|
|
content_type='image/jpeg',
|
|
|
|
content_encoding='binary',
|
2011-11-02 02:39:48 +00:00
|
|
|
routing_key=rkey)
|
2010-11-11 15:00:44 +00:00
|
|
|
|
|
|
|
The `Message` object returned by the `Consumer` class will have a
|
|
|
|
`content_type` and `content_encoding` attribute.
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2012-10-04 15:18:24 +00:00
|
|
|
.. _serialization-entrypoints:
|
2012-09-25 11:21:08 +00:00
|
|
|
|
|
|
|
Creating extensions using Setuptools entry-points
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
A package can also register new serializers using Setuptools
|
|
|
|
entry-points.
|
|
|
|
|
|
|
|
The entry-point must provide the name of the serializer along
|
|
|
|
with the path to a tuple providing the rest of the args:
|
2015-06-12 19:59:52 +00:00
|
|
|
``encoder_function, decoder_function, content_type, content_encoding``.
|
2012-09-25 11:21:08 +00:00
|
|
|
|
|
|
|
An example entrypoint could be:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
from setuptools import setup
|
|
|
|
|
|
|
|
setup(
|
|
|
|
entry_points={
|
|
|
|
'kombu.serializers': [
|
|
|
|
'my_serializer = my_module.serializer:register_args'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Then the module ``my_module.serializer`` would look like:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2015-06-12 19:59:52 +00:00
|
|
|
register_args = (my_encoder, my_decoder, 'application/x-mimetype', 'utf-8')
|
2012-09-25 11:21:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
When this package is installed the new 'my_serializer' serializer will be
|
|
|
|
supported by Kombu.
|
|
|
|
|
|
|
|
|
|
|
|
.. admonition:: Buffer Objects
|
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
The decoder function of custom serializer must support both strings
|
|
|
|
and Python's old-style buffer objects.
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
Python pickle and json modules usually don't do this via its ``loads``
|
|
|
|
function, but you can easily add support by making a wrapper around the
|
|
|
|
``load`` function that takes file objects instead of strings.
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
Here's an example wrapping :func:`pickle.loads` in such a way:
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
.. code-block:: python
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
import pickle
|
2014-05-19 21:27:36 +00:00
|
|
|
from io import BytesIO
|
|
|
|
from kombu import serialization
|
2012-09-25 11:21:08 +00:00
|
|
|
|
|
|
|
|
2012-11-21 16:24:49 +00:00
|
|
|
def loads(s):
|
|
|
|
return pickle.load(BytesIO(s))
|
2012-09-25 11:21:08 +00:00
|
|
|
|
2014-05-19 21:27:36 +00:00
|
|
|
serialization.register(
|
|
|
|
'my_pickle', pickle.dumps, loads,
|
|
|
|
content_type='application/x-pickle2',
|
|
|
|
content_encoding='binary',
|
|
|
|
)
|