r/Python • • Feb 09 '11

Beware of cPickle

http://www.heikkitoivonen.net/blog/2011/02/08/beware-of-cpickle/
1 Upvotes

10 comments sorted by

2

u/[deleted] Feb 09 '11

Man. There's a lot more reasons to beware of pickle and cPickle than that. This is pretty trivial.

2

u/heikkitoivonen Feb 09 '11

Yes there are, but at least all (?) of the other things that I can think of are documented in the pickle module documentation. I would go so far as to call this issue a bug.

1

u/sigzero Feb 10 '11

That is not a bug. It is how it is supposed to work regarding the GIL. It should also be clearly documented but it isn't a bug.

3

u/heikkitoivonen Feb 10 '11

I don't agree. dump and load do release the GIL. dumps and loads could do this as well.

1

u/bryancole Feb 10 '11

How do you know that dump or load is releasing the GIL? I would expect that releasing the GIL in the case of a blocking file IO operation is the responsibility of the file object, not the library that is writing to/from the file.

1

u/heikkitoivonen Feb 10 '11

By looking at the implementation of cPickle, and also by running the simple script that I mentioned in my blog post. It is easy to demonstrate that dumps and loads hold the GIL so that nothing else runs, but just by changing those to dump and load will let other threads run.

1

u/[deleted] Feb 09 '11

This might be surprising at first, but not really surprising if you think about it. While the objects are being deserialized, the unpickler constantly grabs stuff all around, modules, classes, functions, when relinking the instances to their classes and everything that wasn't pickled. If I'm not mistaken, it can actually import necessary modules as well.

So it might seem surprising if you're unpickling a huge list of integers, which could be done in background, sure, but which is not exactly the most common use case of pickle/cpickle.

1

u/heikkitoivonen Feb 10 '11

I don't think that has anything to do with it. cPickle dump and load get around the issue by freeing the GIL for some of the work that they do. dumps and loads could periodically release the GIL, too. Just releasing and immediately reacquiring after something like 1000 pickled objects would let other threads run reasonably well. I actually implemented something like this until I realized that dump and load already release the GIL.

1

u/[deleted] Feb 10 '11

I see, you're right.

-8

u/ipeev Feb 09 '11

There is nothing to beware about. Nobody is writing multithreaded applications in Python.