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

Save a dictionary to a file (alternative to pickle) in Python?

Sure, save it as CSV: import csv w = csv.writer(open(“output.csv”, “w”)) for key, val in dict.items(): w.writerow([key, val]) Then reading it would be: import csv dict = {} for key, val in csv.reader(open(“input.csv”)): dict[key] = val Another alternative would be json (json for version 2.6+, or install simplejson for 2.5 and below): >>> import json … Read more

ValueError: insecure string pickle

“are much more likely than a never-observed bug in Python itself in a functionality that’s used billions of times a day all over the world”: it always amazes me how cross people get in these forums. One easy way to get this problem is by forgetting to close the stream that you’re using for dumping … Read more

multiprocessing.Pool – PicklingError: Can’t pickle : attribute lookup thread.lock failed

multiprocessing passes tasks (which include check_one and data) to the worker processes through a mp.SimpleQueue. Unlike Queue.Queues, everything put in the mp.SimpleQueue must be pickable. Queue.Queues are not pickable: import multiprocessing as mp import Queue def foo(queue): pass pool=mp.Pool() q=Queue.Queue() pool.map(foo,(q,)) yields this exception: UnpickleableError: Cannot pickle <type ‘thread.lock’> objects Your data includes packages, which … Read more

Convert numpy array type and values from Float64 to Float32

The problem is that you do not do any type conversion of the numpy array. You calculate a float32 variable and put it as an entry into a float64 numpy array. numpy then converts it properly back to float64 Try someting like this: a = np.zeros(4,dtype=”float64″) print a.dtype print type(a[0]) a = np.float32(a) print a.dtype … Read more

Python2: Should I use Pickle or cPickle?

The pickle module implements an algorithm for turning an arbitrary Python object into a series of bytes. This process is also called serializing” the object. The byte stream representing the object can then be transmitted or stored, and later reconstructed to create a new object with the same characteristics. The cPickle module implements the same … Read more

What can multiprocessing and dill do together?

multiprocessing makes some bad choices about pickling. Don’t get me wrong, it makes some good choices that enable it to pickle certain types so they can be used in a pool’s map function. However, since we have dill that can do the pickling, multiprocessing’s own pickling becomes a bit limiting. Actually, if multiprocessing were to … Read more

Can’t pickle defaultdict

In addition to Martijn’s explanation: A module-level function is a function which is defined at module level, that means it is not an instance method of a class, it’s not nested within another function, and it is a “real” function with a name, not a lambda function. So, to pickle your defaultdict, create it with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)