Closed GoogleCodeExporter closed 8 years ago
It has nothing to do with the multiprocessing. You can't pickle lambdas, at
least not in that context:
>>> import pickle
>>> f = lambda x: x*x
>>> pickle.dumps(f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/skip/local/lib/python2.7/pickle.py", line 1366, in dumps
Pickler(file, protocol).dump(obj)
File "/Users/skip/local/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/Users/skip/local/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/Users/skip/local/lib/python2.7/pickle.py", line 748, in save_global
(obj, module, name))
PicklingError: Can't pickle <function <lambda> at 0x6683f0>: it's not found as
__main__.<lambda>
However, if you define f using the def statement it works:
>>> def f(x):
... return x*x
...
>>> pickle.dumps(f)
'c__main__\nf\np0\n.'
Original comment by skip.mon...@gmail.com
on 8 Nov 2008 at 12:34
You are right, but if you define the function, it will run on Python3.0 but
will hang
on python 2.5 (at least in my machine). Here is the code I tested:
from multiprocessing import Pool
#from processing import Pool
def f (x):
return x*x
p=Pool(processes=2)
p.map(f,range(1000000))
#print (res)
#res = p.apply_async(f,2)
Original comment by fccoelho
on 8 Nov 2008 at 2:38
Actually you could use a hack to pickle lambda functions. The only thing you are
interested in a function is actually its func_code object, which is
marshallable. So
you can dump lambda functions to a string, see the attached file.
Original comment by Vinzent.Steinberg@gmail.com
on 31 Jan 2010 at 3:39
Attachments:
Original issue reported on code.google.com by
fccoelho
on 7 Nov 2008 at 11:31