I need to boost my python application. The solution was supposed to be trivial:
import time
from multiprocessing import Pool
class A:
    def method1(self):
        time.sleep(1)
        print('method1')
        return 'method1'
    def method2(self):
        time.sleep(1)
        print('method2')
        return 'method2'
    def method3(self):
        pool = Pool()
        time1 = time.time()
        res1 = pool.apply_async(self.method1, [])
        res2 = pool.apply_async(self.method2, [])
        res1 = res1.get()
        res2 = res2.get()
        time2 = time.time()
        print('res1 = {0}'.format(res1))
        print('res2 = {0}'.format(res2))
        print('time = {0}'.format(time2 - time1))
a = A()
a.method3()
But every time I launch this simple program I get an exception:
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.2/threading.py", line 693, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3.2/multiprocessing/pool.py", line 346, in _handle_tasks
    put(task)
_pickle.PicklingError: Can't pickle <class 'method'>: attribute lookup builtins.method failed
It seems python can't parallelize class methods. I have been desperately googling the whole day but I still don't understand how to work it around (possibly I was googling not enough good). Python multiprocessing documentation seems to be very poor.
I don't want to destroy my class separating it on global methods. The following code seems to be workable:
class B:
    def method1(self):
        time.sleep(1)
        print "B.method1"
        return "B.method1"
    def method2(self):
        time.sleep(1)
        print "B.method2"
        return "B.method2"
def method1(b):
    time.sleep(1)
    print('method1')
    return  b.method1()
def method2(b):
    time.sleep(1)
    print('method2')
    return  b.method2()
def method3():
    pool = Pool()
    time1 = time.time()
    b = B()
    res1 = pool.apply_async(method1, [b])
    res2 = pool.apply_async(method2, [b])
    res1 = res1.get()
    res2 = res2.get()
    time2 = time.time()
    print('res1 = {0}'.format(res1))
    print('res2 = {0}'.format(res2))
    print('time = {0}'.format(time2 - time1))
method3()
But I still don't understand how to make python parallelization work inside a class method. Could someone help me to work around this obstacle? Possibly there are other ways of parallelization I don't know that can be applied in this case? I need a workable code as an example. Any help would be greatly appreciated.