I translated a C++ renderer to Python. The C++ renderer uses threads which each render part of the image. I want to do the same thing in Python. It seems, however, that my multi thread code version takes ages compared to my single thread code version. I am new to multiprocessing in Python and was therefore wondering if the code below actually does what I have in mind: creating a pool of threads, adding and executing some tasks and waiting till completion of all of them?
I know that I cannot compete with my C++ version, but I was hoping to beat the single threaded Python version at least.
Multi thread code
from multiprocessing.pool import ThreadPool pool = ThreadPool(processes=4) pool.map(run_task(...), range(11)) pool.close() pool.join() Single thread code
for i in range(11): Task(...)(i) Task code
def run_task(...): task = Task(...) return task.__call__ class Task(): def __init__(self, ...): ... def __call__(self, i): ... Edit: I tried to use from multiprocessing import Pool. This seems to block my Python terminal in Canopy IDE. When I run the file from the Windows commandline, I receive:
C:\Users\Matthias\Documents\Courses\Masterproef\pbrt\Tools\Permeability\src>pyth on renderer.py Exception in thread Thread-2: Traceback (most recent call last): File "C:\Users\Matthias\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5. 2.2785.win-x86_64\lib\threading.py", line 810, in __bootstrap_inner self.run() File "C:\Users\Matthias\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5. 2.2785.win-x86_64\lib\threading.py", line 763, in run self.__target(*self.__args, **self.__kwargs) File "C:\Users\Matthias\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.5. 2.2785.win-x86_64\lib\multiprocessing\pool.py", line 342, in _handle_tasks put(task) PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin_ _.instancemethod failed (This is also why I prefer threads over processes in general. So the GIL design decision makes not really sense to me.)