This is the error I got today at http://filmaster.com">filmaster.com:
PicklingError: Can't pickle : it's not the same object as decimal.Decimal
What does that exactly mean? It does not seem to be making a lot of sense... It seems to be connected with django caching. You can see the whole traceback here:
Traceback (most recent call last):
File "/home/filmaster/django-trunk/django/core/handlers/base.py", line 92, in get_response response = callback(request, *callback_args, **callback_kwargs)
File "/home/filmaster/film20/film20/core/film_views.py", line 193, in show_film
workflow.set_data_for_authenticated_user()File "/home/filmaster/film20/film20/core/film_views.py", line 518, in set_data_for_authenticated_user
object_id = self.the_film.parent.id)File "/home/filmaster/film20/film20/core/film_helper.py", line 179, in get_others_ratings
set_cache(CACHE_OTHERS_RATINGS, str(object_id) + "_" + str(user_id), userratings)File "/home/filmaster/film20/film20/utils/cache_helper.py", line 80, in set_cache return cache.set(CACHE_MIDDLEWARE_KEY_PREFIX + full_path, result, get_time(cache_string))
File "/home/filmaster/django-trunk/django/core/cache/backends/memcached.py", line 37, in set
self._cache.set(smart_str(key), value, timeout or self.default_timeout)File "/usr/lib/python2.5/site-packages/cmemcache.py", line 128, in set val, flags = self._convert(val)
File "/usr/lib/python2.5/site-packages/cmemcache.py", line 112, in _convert val = pickle.dumps(val, 2)
PicklingError: Can't pickle : it's not the same object as decimal.Decimal
And the source code for Filmaster can be downloaded from here: bitbucket.org/filmaster/filmaster-test
Any help will be greatly appreciated.
I will demonstrate the problem with simple Python classes in Python2.7:
This error is shown because we are trying to dump A, but because we changed its name to refer to another object "B", pickle is actually confused with which object to dump - class A or B. Apparently, pickle guys are very smart and they have already put a check on this behavior.
Solution: Check if the object you are trying to dump has conflicting name with another object.
I have demonstrated debugging for the case presented above with ipython and ipdb below:
I hope this saves some headaches! Adios!!
I can't explain why this is failing either, but my own solution to fix this was to change all my code from doing
to
this one change and it worked. I'd love to know why... hth
I got this error when running in an jupyter notebook. I think the problem was that I was using
%load_ext autoreload
autoreload 2
. Restarting my kernel and rerunning solved the problem.One oddity of Pickle is that the way you import a class before you pickle one of it's instances can subtly change the pickled object. Pickle requires you to have imported the object identically both before you pickle it and before you unpickle it.
So for example:
will make a subtly different object (sometimes) to:
Try fiddling with your imports, it might correct the problem.
Did you somehow
reload(decimal)
, or monkeypatch the decimal module to change the Decimal class? These are the two things most likely to produce such a problem.There can be issues starting a process with
multiprocessing
by calling__init__
. Here's a demo:Now, with the above code, if we run this:
We get this error:
And if we change it to use
fork
instead ofspawn
:We get this error:
But if we call the
start_process
method, which doesn't call__init__
in themp.Process
target, like this:It works as expected (whether we use
spawn
orfork
).