I recently saw a answer/comment about how functions are objects in python. So, I wondering why when I take that example, and create a class around it while initializing a variable, it doesn't work the same way. (The class example receives a pickling error):
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
Does anyone know why this is?
Example code from the link:
import multiprocessing as mp
def f(x):
f.q.put('Doing: ' + str(x))
return x*x
def f_init(q):
f.q = q
def main():
jobs = range(1,6)
q = mp.Queue()
p = mp.Pool(None, f_init, [q])
results = p.imap(f, jobs)
p.close()
for i in range(len(jobs)):
print q.get()
print results.next()
if __name__ == '__main__':
main()
Same example while puttin f
into a class:
import multiprocessing as mp
class F:
def __init__(self, q):
self.q = q
def f(x):
self.q.put('Doing: ' + str(x))
return x*x
def main():
jobs = range(1,6)
q = mp.Queue()
p = mp.Pool(None)
f = F(q)
results = p.imap(f.f, jobs)
p.close()
for i in range(len(jobs)):
print q.get()
print results.next()
if __name__ == '__main__':
main()