Python Multiprocessing Lib Error (AttributeError: __exit__)

In Python 2.x and 3.0, 3.1 and 3.2, multiprocessing.Pool() objects are not context managers. You cannot use them in a with statement. Only in Python 3.3 and up can you use them as such. From the Python 3 multiprocessing.Pool() documentation: New in version 3.3: Pool objects now support the context management protocol – see Context … Read more

installing cPickle with python 3.5

cPickle comes with the standard library… in python 2.x. You are on python 3.x, so if you want cPickle, you can do this: >>> import _pickle as cPickle However, in 3.x, it’s easier just to use pickle. No need to install anything. If something requires cPickle in python 3.x, then that’s probably a bug.

Preserve custom attributes when pickling subclass of numpy array

np.ndarray uses __reduce__ to pickle itself. We can take a look at what it actually returns when you call that function to get an idea of what’s going on: >>> obj = RealisticInfoArray([1, 2, 3], info=’foo’) >>> obj.__reduce__() (<built-in function _reconstruct>, (<class ‘pick.RealisticInfoArray’>, (0,), ‘b’), (1, (3,), dtype(‘int64’), False, ‘\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00’)) So, we get a 3-tuple … Read more

Why can I pass an instance method to multiprocessing.Process, but not a multiprocessing.Pool?

The pickle module normally can’t pickle instance methods: >>> import pickle >>> class A(object): … def z(self): print “hi” … >>> a = A() >>> pickle.dumps(a.z) Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/usr/local/lib/python2.7/pickle.py”, line 1374, in dumps Pickler(file, protocol).dump(obj) File “/usr/local/lib/python2.7/pickle.py”, line 224, in dump self.save(obj) File “/usr/local/lib/python2.7/pickle.py”, line … Read more

How to read pickle file?

Pickle serializes a single object at a time, and reads back a single object – the pickled data is recorded in sequence on the file. If you simply do pickle.load you should be reading the first object serialized into the file (not the last one as you’ve written). After unserializing the first object, the file-pointer … Read more