Multiple independent Python interpreters in a C/C++ program?

Matimus mccredie at gmail.com
Fri Apr 11 13:22:18 EDT 2008


On Apr 11, 9:24 am, s... at pobox.com wrote:
> This question was posed to me today.  Given a C/C++ program we can clearly
> embed a Python interpreter in it.  Is it possible to fire up multiple
> interpreters in multiple threads?  For example:
>
>     C++ main
>         thread 1
>             Py_Initialize()
>         thread 2
>             Py_Initialize()
>
> Do I wind up with two completely independent interpreters, one per thread?
> I'm thinking this doesn't work (there are bits which aren't thread-safe and
> are only protected by the GIL), but wanted to double-check to be sure.
>
> Thanks,
>
> Skip

AFAIK it is only `sort of' possible. But not like that. See the API
documentation on Py_NewInterpreter http://www.python.org/doc/api/initialization.html#l2h-825

As you will see from the documentation it creates an interpreter that
isn't 100% separate. It may work for your application though.

Also, I don't think using the new interpreter is as simple as just
instantiating the new interpreter in a separate thread. But there is
much relevant information on the page that I linked to.

Matt



More information about the Python-list mailing list