[Python-Dev] Feature Request: Py_NewInterpreter to create separate GIL (branch)

Brett Cannon brett at python.org
Sat Nov 4 08:20:23 CET 2006

On 11/3/06, Robert <kxroberto at googlemail.com> wrote:
> repeated from c.l.p : "Feature Request: Py_NewInterpreter to create
> separate GIL (branch)"
> Daniel Dittmar wrote:
> > robert wrote:
> >> I'd like to use multiple CPU cores for selected time consuming Python
> >> computations (incl. numpy/scipy) in a frictionless manner.
> >>
> >> Interprocess communication is tedious and out of question, so I
> >> thought about simply using a more Python interpreter instances
> >> (Py_NewInterpreter) with extra GIL in the same process.
> >
> > If I understand Python/ceval.c, the GIL is really global, not specific
> > to an interpreter instance:
> > static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
> >
> Thats the show stopper as of now.
> There are only a handfull funcs in ceval.c to use that very global lock.
> The rest uses that funcs around thread states.
> Would it be a possibilty in next Python to have the lock separate for
> each Interpreter instance.
> Thus: have *interpreter_lock separate in each PyThreadState instance and
> only threads of same Interpreter have same GIL?
> Separation between Interpreters seems to be enough. The Interpreter runs
> mainly on the stack. Possibly only very few global C-level resources
> would require individual extra locks.

Right, but that's the trick.  For instance extension modules are shared
between interpreters.  Also look at the sys module and basically anything
that is set by a function call is a process-level setting that would also
need protection.  Then you get into the fun stuff of the possibility of
sharing objects created in one interpreter and then passed to another that
is not necessarily known ahead of time (whether it be directly through C
code or through process-level objects such as an attribute in an extension

It is not as simple, unfortunately, as a few locks.

Sooner or later Python will have to answer the multi-processor question.
> A per-interpreter GIL and a nice module for tunneling Python-Objects
> directly between Interpreters inside one process might be the answer at
> the right border-line ? Existing extension code base would remain
> compatible, as far as there is already decent locking on module globals,
> which is the the usual case.

This is not true (see above).  From my viewpoint the only way for this to
work would be to come up with a way to wrap all access to module objects in
extension modules so that they are not trampled on because of separate locks
per-interpreter, or have to force all extension modules to be coded so that
they are instantiated individually per interpreter.  And of course deal with
all other process-level objects somehow.

The SMP issue for Python will most likely not happen until someone cares
enough to write code to do it and this take on it is no exception.  There is
no simple solution or else someone would have done it by now.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20061103/9ca05403/attachment.html 

More information about the Python-Dev mailing list