A colleague and are attempting to add python scripting support to a win forms application developed in .Net 2.0. The initial prototyping went well but the new scripting components stopped working once we integrated it into the main application. We think we've isolated the problem to either a problem with Python.Net, the python implementation we are using, or how we invoke the python scripts from within the background worker thread.
Here is a simplified code snippet that recreates the issue we are seeing:
As you can see in the code above, I first call the foo method from the main thread. I then invoke a worker thread and in that thread, create a new pyObject and call the bar method from the python script.
The problem occurs on the last line of the workerThreadProcessing. When this line is executed, the application hangs indefinitely. The catch is that if I comment out the contents of "SECTION 1", bar completes as desired. This seems to indicate that there is something in "SECTION 1" that is causing the problem.
The python script, sample.py is trivial:
We have a similar troubleshooting thread going through the python.net mailing list right now to determine if there is a limitation in python.net but I am concerned that we've missed something important on the .Net side. Are there any known issues with the background worker that might be causing a problem? Have I made some kind of fundamental mistake that I'm just not seeing?
Any help or insight (on either side of this problem) is greatly appreciated.
Here is a simplified code snippet that recreates the issue we are seeing:
Code:
PyObject pyPlugin;
string pathToScript;
private void button1_Click(object sender, EventArgs e)
{
//*************** START SECTION 1 ****************************
pathToScript = @"C:\Dev\pyScripts\sample.py";
Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(pathToScript));
PythonEngine.Initialize();
string oldWorkingDirectory = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.GetDirectoryName(pathToScript));
pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(pathToScript));
Directory.SetCurrentDirectory(oldWorkingDirectory);
MessageBox.Show(pyPlugin.GetAttr("foo").Invoke().ToString());
//*************** END SECTION 1 *****************************
BackgroundWorker workerThread = [URL="http://www.google.com/search?q=new+msdn.microsoft.com"]new[/URL] BackgroundWorker();
workerThread.DoWork += workerThreadProcessing;
workerThread.RunWorkerAsync();
}
public void workerThreadProcessing(object sender, EventArgs e)
{
pathToScript = @"C:\Dev\pyScripts\sample.py";
Environment.SetEnvironmentVariable("PYTHONHOME", Path.GetDirectoryName(pathToScript));
PythonEngine.Initialize();
string oldWorkingDirectory = Directory.GetCurrentDirectory();
Directory.SetCurrentDirectory(Path.GetDirectoryName(pathToScript));
pyPlugin = PythonEngine.ImportModule(Path.GetFileNameWithoutExtension(pathToScript));
Directory.SetCurrentDirectory(oldWorkingDirectory);
MessageBox.Show(pyPlugin.GetAttr("bar").Invoke().ToString());
}
As you can see in the code above, I first call the foo method from the main thread. I then invoke a worker thread and in that thread, create a new pyObject and call the bar method from the python script.
The problem occurs on the last line of the workerThreadProcessing. When this line is executed, the application hangs indefinitely. The catch is that if I comment out the contents of "SECTION 1", bar completes as desired. This seems to indicate that there is something in "SECTION 1" that is causing the problem.
The python script, sample.py is trivial:
Code:
def foo():
return "foo has been called"
def bar():
return "bar has been called"
Any help or insight (on either side of this problem) is greatly appreciated.