Search code examples
pythonmultithreadingimage-manipulation

practice with threads in python


I know that Python has a global lock and i've read Glyph's explaination of python multithreading. But I still want to try it out. What I decided to do as an easy (conceptually) task was to do horizontal and vertical edge detection on a picture.

Here's what's happening (pseudocode):

for pixels in picture:
    apply sobel operator horizontal
for pixels in picture:
    apply sobel operator vertical

info on sobel operator.

These two loops can run completely independent of each other, and so would be prime candidates for multithreading. (running these two loops on any significantly large picture can take 10+ seconds). However, when I have tried to use the threading module in python, it takes twice as long because of the global lock. My question is should I abandon all hope of doing this in two threads in python and try in another language? If i can forge ahead, what module(s) should I use? If not, what language should I experiment in?


Solution

  • Python 2.6 now includes the mulitprocessing module (formerly processing module on older versions of Python).

    It has essentially the same interface as the threading module, but launches the execution into separate processes rather than threads. This allows Python to take advantage of multiple cores/CPUs and scales well for CPU-intensive tasks compared to the threading module approach.