Safe to call Py_Initialize() frequently?

Graham Dumpleton Graham.Dumpleton at gmail.com
Sun Mar 22 06:33:53 EDT 2009


On Mar 21, 2:35 pm, roschler <robert.osch... at gmail.com> wrote:
> On Mar 20, 7:27 pm, Mark Hammond <skippy.hamm... at gmail.com> wrote:
>
> > On 21/03/2009 4:20 AM, roschler wrote:
>
> > Calling Py_Initialize() multiple times has no effect.  Calling
> > Py_Initialize and Py_Finalize multiple times does leak (Python 3 has
> > mechanisms so this need to always be true in the future, but it is true
> > now for non-trivial apps.
>
> > > If it is not a safe approach, is there another way to get what I want?
>
> > Start a new process each time?
>
> > Cheers,
>
> > Mark
>
> Hello Mark,
>
> Thank you for your reply.  I didn't know that Py_Initialize worked
> like that.
>
> How about using Py_NewInterpreter() and Py_EndInterpreter() with each
> job?  Any value in that approach?  If not, is there at least a
> reliable way to get a list of all active threads and terminate them so
> before starting the next job?  Starting a new process each time seems
> a bit heavy handed.

Using Py_EndInterpreter() is even more fraught with danger. The first
problem is that some third party C extension modules will not work in
sub interpreters because they use simplified GIL state API. The second
problem is that third party C extensions often don't cope well with
the idea that an interpreter may be destroyed that it was initialised
in, with the module then being subsequently used again in a new sub
interpreter instance.

Given that it is one operation per second, creating a new process, be
it a completely fresh one or one forked from existing Python process,
would be simpler.

Graham




More information about the Python-list mailing list