Python 2.7和3.7.2兼容Django-REDIS序列化器



我正在尝试编写py2.7 -py3.7兼容django -redis serialializer。我将django-redis==4.8.0django==1.11.22PickleSerializer一起使用。我在django-redis上看到了这个问题https://github.com/niwinz/django-redis/pull/279,并编写了类似于线程中所述内容的序列化器。但是我的对象似乎更复杂吗?不确定。

我的目标是同时运行2个应用程序,一个应用程序具有py2.7,另一个则为py3.7。他们必须100%兼容,我无法克服这一点。

这是序列化器的代码:

# -*- coding: utf-8 -*-
import six
from django.utils.encoding import force_bytes
from django_redis.serializers.pickle import PickleSerializer
try:
    import cPickle as pickle
except ImportError:
    import pickle

class CompatPickleSerializer(PickleSerializer):
    def loads(self, value):
        if six.PY3:
            return self._loads_py3(value)
        return super(CompatPickleSerializer, self).loads(force_bytes(value))
    def _loads_py3(self, value):
        return pickle.loads(
            force_bytes(value),
            fix_imports=True,
            encoding='bytes'
        )

我试图序列化的对象的示例:

{
    'created_at': datetime.datetime(2019, 7, 30, 20, 0, 29, 244916, tzinfo = < UTC > ),
    'items': [{
        'unit_price': Decimal('3.00'),
        'name': 'my item',
        'id': '12312312',
    }]
    'id': 'b5c6210d-561f-4e4e-a025-e55b39d95418',
    'name': 'cart',
    'customer': None,
}

该对象比那大得多,但是我认为,如果我可以用这个对象进行流,我可以使用一个更大的对象。

尝试 load python 3.7.2上的对象后,我得到了此错误:

Traceback:  
File "my-project/lib/python3.7/site-packages/django_redis/client/default.py" in decode
  313.             value = int(value)

      During handling of the above exception (invalid literal for int() with base 10: b'x80x02}qx01(Utdiscountsqx02NUx10display_order_idqx03NUx12shipping_method_idqx04NUx0creservationsqx05}Uncreated_atqx06U 2019-07-30T20:00:14.022071+00:00qx07Utpromocodeqx08NUx11shippi), another exception occurred:

File "my-project/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)
File "my-project/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)
File "my-project/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "my-project/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "my-project/lib/python3.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "my-project/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  489.             response = self.handle_exception(exc)
File "my-project/lib/python3.7/site-packages/rest_framework/views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)
File "my-project/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)
File "django/cart/api/cart.py" in get
  98.         cart = Cart.get(cart_id)
File "django/cart/models/cart.py" in get
  1.190.             raise e
File "django/cart/models/cart.py" in get
  1.186.             data = cart_cache.get(id)
File "my-project/lib/python3.7/site-packages/django_redis/cache.py" in _decorator
  33.             return method(self, *args, **kwargs)
File "my-project/lib/python3.7/site-packages/django_redis/cache.py" in get
  82.                                    client=client)
File "my-project/lib/python3.7/site-packages/django_redis/client/default.py" in get
  208.         return self.decode(value)
File "my-project/lib/python3.7/site-packages/django_redis/client/default.py" in decode
  320.             value = self._serializer.loads(value)
File "django/backports/django_redis/serializers.py" in loads
  28.             return self._loads_py3(value)
File "django/backports/django_redis/serializers.py" in _loads_py3
  35.             encoding='bytes'
Exception Type: TypeError at /my-url/
Exception Value: conversion from bytes to Decimal is not supported

关于我能做什么的任何想法?

如果有人有这个问题,我来了这个解决方案

# -*- coding: utf-8 -*-
import six
from django_redis.serializers.pickle import PickleSerializer, pickle

class CompatPickleSerializer(PickleSerializer):
    def loads(self, value):
        if six.PY3:
            return self._loads_py3(value)
        return super(CompatPickleSerializer, self).loads(value)
    def _loads_py3(self, value):
        return pickle.loads(
            value,
            fix_imports=True,
            encoding='latin1'
        )

我从 encoding='bytes'更改为 latin1,并且起作用。无需强制。

另一个重要的事情是您需要将PICKLE_VERSION强加于2,否则您将在Python2.7上腌制数据,如果它在Python3上进行了序列化。

希望它对任何人有帮助。

相关内容

  • 没有找到相关文章

最新更新