// In the main thread Pythonnet is initialised like this:
public void Initialise()
PythonEngine.Initialize();
m_outerScope = Py.CreateScope( "outerscope" )
m_mainThreadState = PythonEngine.BeginAllowThreads();
// A python script is run on a worker thread like this.
// pyObj is a CLR object with methods the script can call, created
// with clrobj.ToPython():
void runScript( string script, PyObject pyObj, string objName)
using( Py.GIL() ) {
using( PyScope scope = m_outerScope.NewScope() ) {
try {
scope.Set( objName, pyObj );
scope.Exec( script );
} catch( Exception exc ) {
// Within the Python script we have something like:
objName.SomeMethod( 'message')
// The callback method on the CLR object looks like: