views:

100

answers:

2

I have windows 7 and I wrote a python program that loops ("for loops", i.e., "for key in dict") over multiple databases, checks for various conditions (e.g., if x in dict, y += 1) and then tallies the results. I didn't do anything to parralelize the proceesing. I have 8 CPU cores on my computer. When I start Windows task manager while running the program, it shows substantial activity on all 8 cores. Is windows executing the for loops in parralel on multiple proceesors without me doing anything? The code executes fast.

+4  A: 

No. The C implementation of Python only allows one thread to interpret one bytecode at a time. The only way to take advantage of multiple cores is with multiple threads or multiple processes.

It is completely possible that you will see multiple core activity related to your script however. Let's say you open several files for reading or writing. The OS will buffer the read / write calls that Python is making (on behalf of your script) and will return control to the next statement before that statement is complete in some cases. This is the OS rather than Python making that optimization however.

The Java implementation of Python will do what ever the native version of Java will do on your OS.

Here is a more complete discussion of the same topic.

drewk
+2  A: 

The answer to your question is "no" -- whatever's happening on 7 of your 8 cores, it's Windows doing its things (services and whatnot) "in the background". When you do want to parallelize over multiple cores, you'll need to use Python standard library's multiprocessing module (there are other ways, but none quite as convenient as simple as this).

Alex Martelli