Sunday, February 1, 2009

Python Interpreter inside TinkerCell

Problem:
It is not a good idea to load and unload the Python interpreter. It can cause Python to crash if external modules are loaded. 

The DynamicCodePlugin has been modified to alleviate this issue. 

The solution:  
The PythinInterpreterThread class runs as a separate thread. It initializes the Python interpreter when TinkerCell is loaded, and it finalizes (i.e. quits) the interpreter when TinkerCell exists. 

Now, it is very easy to call Python code inside a C program. The hillEquations.c function that generates the automatic Hill equation calls a Python code like this:

#include "TC_api.h"

int main()
{
    tc_runPythonFile("c/hillEquations.py");
    return 0;
}

Simple!

What happens here is that the DynamicCodePlugin accepts the string and sends it to the already loaded Python interpreter. This makes the process fast because the interpreter is already loaded. 

Drawback:

The Python interpreter runs as a single separate thread. This means that only one Python program can run at a time. In other words, Python programs are not as multi-threaded as C programs. But it is probably rare that someone would need to run multiple Python programs at once. 



No comments: