Safe to call Py_Initialize() frequently?

Mark Hammond skippy.hammond at
Sat Mar 21 00:27:13 CET 2009

On 21/03/2009 4:20 AM, roschler wrote:
> I've created a Python server that embeds Python 2.5 and runs Python
> jobs.  I want to be able to completely "flush" the interpreter between
> each job.  That means resetting all variables, stopping all user
> created threads, and resetting the interpreter sys module path.  If it
> does not cause memory leaks, slowdowns, or other problems I would like
> to call Py_Initialize() before running each job.  I expect to run a
> job about once a second.  Are there any known issues with doing this
> or anything else that would make this a bad approach?

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 a safe approach, do I have to pair each Py_Initialize() call
> with a Py_Finalize() call?
> If it is not a safe approach, is there another way to get what I want?

Start a new process each time?



More information about the Python-list mailing list