I am using the Python multiprocessing module to place objects onto a queue and have them processed by several workers. My first issue was getting bound instance methods to pickle, which I have working, but now I'm running into a separate issue caused by the fact that the objects are using __slots__
.
When the mp module goes to pickle the objects, it seems to be using the older ascii pickle protocol, that can't handle __slots__
. The newer protocol does handle this, but I'm not sure how to make the mp module use this protocol.
Anyone have any experience with this?
However, the multiprocess tasks can't be pickled; it would raise an error failing to pickle. That's because when dividing a single task over multiprocess, these might need to share data; however, it doesn't share memory space.
The pickle module is not secure. Only unpickle data you trust. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with.
The pickle module is used for implementing binary protocols for serializing and de-serializing a Python object structure. Pickling: It is a process where a Python object hierarchy is converted into a byte stream.
To use pickle, start by importing it in Python. To pickle this dictionary, you first need to specify the name of the file you will write it to, which is dogs in this case. Note that the file does not have an extension. To open the file for writing, simply use the open() function.
If it's not possible to change the pickle protocol the multiprocessing package uses, then define __getstate__
and __setstate__
for your objects:
import pickle
class Foo(object):
__slots__ = ['this', 'that', 'other']
def __init__(self):
self.this = 1
self.that = 2
self.other = 3
def __getstate__(self):
return dict((name, getattr(self, name))
for name in self.__slots__)
def __setstate__(self, state):
for name, value in state.items():
setattr(self, name, value)
pickle.dumps(Foo(), protocol=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With