using threads with for-loops

Alex Martelli aleaxit at yahoo.com
Wed Sep 29 09:50:44 EDT 2004


Mathias Waack <M.Waack at gmx.de> wrote:
   ...
> >> Have you ever seen machines with more than one CPU?
> > 
> > Why yes, I have!  And have *you* seen an implementation
> > of Python which will effectively use those multiple CPUs
> > in code like that above which runs in a single process?
> 
> Ok, I've lost: I haven't seen such implementation and don't know much
> about the thread-layer of Python. 

There are, as far as I know, three complete implementations of Python
(plus several add-on bits and pieces and unfinished ones): CPython,
Jython, and IronPython.  CPython uses its own dedicated virtual machine,
and its threads are subject to a global per-interpreter lock.

However, in lieu of dedicated virtual machines, Jython relies on the
JVM, and IronPython relies on Microsoft CLR's, and I believe both of
those VMs have no global interpreter lock.  I have no multi-CPU machine
at hand that can run Microsoft's CLR, but I do have a Powermac with two
CPUs, MacOSX 10.3.5, and a JVM (1.4.2 is the latest one, I believe).
So, if you can suggest a test to show whether Jython there can in fact
effectively use both CPU's, I'll be glad to run it and let everybody
know (I'm a bit rusty on recent Java VMs, so I don't know if I need any
special incantations to tell them to run on many CPUs, or what).  I'm
not sure IronPython runs fully on Mono, and neither am I sure the
current release of Mono on MacOSX is able to use multiple CPUs for
threading, but if somebody can find out and suggest a definitive test on
the matter, again I'll be glad to run it and report to the list.

Net of such niggling issues, one might say that _most_ (hey, 2 out of 3,
right?-) current complete implementations of Python can do "free
threading" with no global per-interpreter lock, and thus in theory
should be able to use multiple CPUs productively in multiple CPU-bound
threads of a sinble process -- assuming, say, Java or C# can do so, I
see no reason, in principle, why Python shouldn't be able to, when it
runs on the same underlying VM as Java or C# respectively.


> >> The code above is perfectly suited for parallelization.
> > 
> > Yes, it is.  Once you take into account implementation
> > issues (e.g. the GIL), would you still think so?
> 
> Depends. Which means: don't know. If I would start thinking about
> creating threads to gain a speedup, I would even think about
> switching to another programming language. 

...or another implementation of Python, if you're currently using
CPython and some limitation in it is a big problem for you...


Alex



More information about the Python-list mailing list