I have a function that is side effect free and I would like to run for every element in an array and return an array with all of the results. I'm wondering if python has something built into to generate all of the values.
Thank you.
I have a function that is side effect free and I would like to run for every element in an array and return an array with all of the results. I'm wondering if python has something built into to generate all of the values.
Thank you.
Try the Pool.map function from multiprocessing:
http://docs.python.org/library/multiprocessing.html#using-a-pool-of-workers
It's not multithreaded per-se, but that's actually good since multithreading is severely crippled in Python by the GIL.
You can try OpenMp, though I don't know if there is a native python interface. You would probably need to write the routine in C/C++ and use swig to call it. OpenMp is very use to use for this sort of loop parallelism and is used by issuing compiler directives.
You can use the multiprocessing python package (http://docs.python.org/library/multiprocessing.html). The cloud python package, available from PiCloud (http://www.picloud.com), offers a multi-processing map() function as well, which can offload your map to the cloud.
This functionality is not built in. However, someone has already implemented it.
I would think there would be no reason to have such a function. All Python threads have to execute on the same CPU. Assuming your map function has no I/O component, you would not see any speedup in processing (and would probably see a slowdown due to context switching).
Other posters have mentioned multiprocessing - that is probably a better idea.
Maybe try the Unladen Swallow Python 3 implementation? That might be a major project, and not guaranteed to be stable, but if you're inclined it could work. Then list or set comprehensions seem like the proper functional structure to use.