Multiprocessing: How to use Pool.map on a function defined in a class?

When I run something like: from multiprocessing import Pool p = Pool(5) def f(x): return x*x p.map(f, [1,2,3]) it works fine. However, putting this as a function of a class: class calculate(object): def run(self): def f(x): return x*x p = Pool() return p.map(f, [1,2,3]) cl = calculate() print cl.run() Gives me the following error: Exception … Read more

Can’t pickle when using multiprocessing Pool.map()

I’m trying to use multiprocessing‘s Pool.map() function to divide out work simultaneously. When I use the following code, it works fine: import multiprocessing def f(x): return x*x def go(): pool = multiprocessing.Pool(processes=4) print pool.map(f, range(10)) if __name__== ‘__main__’ : go() However, when I use it in a more object-oriented approach, it doesn’t work. The error … Read more

Using pickle.dump – TypeError: must be str, not bytes

I’m using python3.3 and I’m having a cryptic error when trying to pickle a simple dictionary. Here is the code: import os import pickle from pickle import * os.chdir(‘c:/Python26/progfiles/’) def storvars(vdict): f = open(‘varstor.txt’,’w’) pickle.dump(vdict,f,) f.close() return mydict = {‘name’:’john’,’gender’:’male’,’age’:’45’} storvars(mydict) and I get: Traceback (most recent call last): File “C:/Python26/test18.py”, line 31, in <module> … Read more

How can I use pickle to save a dict (or any other Python object)?

I have looked through the information that the Python docs give, but I’m still a little confused. Could somebody post sample code that would write a new file then use pickle to dump a dictionary into it? 1Best Answer 11 Try this: import pickle a = {‘hello’: ‘world’} with open(‘filename.pickle’, ‘wb’) as handle: pickle.dump(a, handle, … Read more