0

I've got a program that combines multiple(up to 48 images) that are all color coded images. Only running through 32 of the 1024x768 images takes like 7.5 minutes to process through the images and give the output image. I'm doing each image separately using getdata(wxPython) and then going through each image pixel by pixel to get the underlying value and adding it to the final image list before creating/displaying the final image. The entire program works just like I want it to work with one SLOW exception. It takes 7.5 minutes to complete to run. If I want to do anything slightly different I have to start all over.

What I'm wandering if I were to go through the images using threading that should save time but I'm concerned about the possibility of running into one problem...calling the same exact code at the same time by different threads. Is that something to be worried about or not? As well is there something I'm not thinking about that I should be worried about?

confused
  • 1,283
  • 6
  • 21
  • 37
  • You should read things like http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies?rq=1 before asking. Short answer: There is no problems using simultaneously the same *code*. There is a big problem modifying simultaneously the same *data*. – hivert Feb 15 '14 at 15:11
  • I always try a simple search as I start a question and pretty much everything was coming back with anything but python related subjects. Thanks for the link. Sometimes the easiest answers can be the hardest to find if you don't put in the right question to start with. I was thinking of data updating at the same time being a problem as well but I forgot to mention it. If I were to go out and put in a pause(1-2 seconds) between each thread start, since each thread is running the same pixel mapping routine, then would I be okay with the concept? – confused Feb 15 '14 at 16:00
  • Let me add this in as well. If I am doing the above using self.x, self.y does that 'qualify' as the same data or is the list(pixels) I store the result in being qualified as the data. – confused Feb 15 '14 at 16:02
  • You should almost never be going through images "pixel by pixel", let a library written in C do that for you: Numpy for simple things, Pillow for more complicated ones. C code (that you never have to see or touch) is often vectorized leading to **massive** speed gains. – Nick T Feb 15 '14 at 17:24

1 Answers1

0

Read about GIL - threading won't do the trick with your problem. It does it, when you can represent your computation as some kind of pipeline, or when it uses a lot of IO (because only then synchronization will be useful, if you want to run the same thing on several threads instead of single threaded loop, you'll end up with random order of computations, one thread at the time).

You probably want to map your data (maybe parts of picture, maybe whole pictures) to many processes, as they can work in parallel, without taking care of GIL, because they will be different interpreter processes. You can use Pool here.

Filip Malczak
  • 3,124
  • 24
  • 44
  • This doesn't answer the question, albeit relevant to the OP issue. I suggest you to *also* include the answer to the question that the OP *actually* asked. – Bakuriu Feb 15 '14 at 15:37