[Python-ideas] Python and Concurrency
jcarlson at uci.edu
Thu Mar 22 23:40:27 CET 2007
Ron Adam <rrr at ronadam.com> wrote:
> Josiah Carlson wrote:
> > But it's not about threads, it is about concurrent execution of code
> > (which threads in Python do not allow). The only way to allow this is
> > to basically attach a re-entrant lock on every single Python object
> > (depending on the platform, perhaps 12 bytes minimum for count, process,
> > thread). The sheer volume of the number of acquire/release cycles
> > during execution is staggering (think about the number of incref/decref
> > operations), and the increase in size of every object by around 12 bytes
> > is not terribly appealing.
> > On the upside, this is possible (change the PyObject_HEAD macro,
> > PyINCREF, PyDECREF, remove the GIL), but the amount of work necessary to
> > actually make it happen is huge, and it would likely result in negative
> > performance until sheer concurrency wins out over the acquire/release
> > overhead.
> It seems to me some types of operations are more suited for concurrent
> operations than others, so maybe new objects that are designed to be
> naturally usable in this way could help. Or maybe there's a way to lock
> groups of objects at the same time by having them share a lock if they are
That is a fine-grained vs. coarse-grained locking argument. There is
On the other hand, there is also the pi-calculus:
Of course, the pi-calculus is not reasonable unless one starts with a
lambda calculus and decides to modify it (parallel lisp?), so it isn't
all that applicable here.
> Thinking out loud of ways a python program may use concurrent processing:
> * Applying a single function concurrently over a list. (A more limited
> function object might make this easier.)
> * Feeding a single set of arguments concurrently over a list of callables.
> * Generators with the semantics of calculating first and waiting on 'yield'
> for 'next', so the value is immediately returned. (depends on CPU load)
> * Listcomps that perform the same calculation on each item may be a natural
> multi-processing structure.
These examples are all what are generally referred to as "embarassingly
parallel" in literature. One serious issue with programming parallel
algorithms generally is that not all algorithms are necessarily
parallelizable. Some are, certainly, but not all. The task is to
discover those alternate algorithms that *are* parallelizable in such a
way to offer gains that are "worth it".
Personally, I think that if there were a *cheap* IPC to make
cross-process calls not too expensive, many of the examples above that
you talk about would be handled easily.
More information about the Python-ideas