views:

94

answers:

1

I have a WPF app that controls audio hardware. It uses the same PythonEngine on multiple threads. This causes strange errors I see from time to time where the PythonEngines Globals dictionary has missing values. I am looking for some guidance on how to debug/fix this.

The device has multiple components [filter's, gain's, etc.]. Each component has multiple controls [slider's,togglebutton's, etc.].

Everytime a user changes a control value a python script (from the hardware vendor) needs to run. I am using IronPython 1.1.2(PythonEngine.Execute(code)) to do this.

Every component has a script. And each script requires the current values of all controls (of that component) to run.

The sequence is - user makes change > run component script > send results to device > check response for failure. This whole cycle takes too long to keep the UI waiting so everytime something changes I do something like component.begininvoke(startcycle).

Startcycle looks something like this -

PyEngine Engine = PyEngine.GetInstance(); // this is a singleton 
lock(component) // this prevents diff controls of the same component from walking over each other
{
  Engine.runcode(...)
}

When different component.begininvokes happen close to each other there are chances where engine.runcode is happening on different threads at the same time. It looks like I need to get rid of the component.begininvoke but that would make things crawl. Any ideas?

A: 

You probably want to create a EngineModule for each execution and execute the code against that. Then all of the code will run against a different set of variables. You also probably want to get a CompiledCode object and actually execute that against the new EngineModule each time because engine.Execute will need to re-compile it each time.

Dino Viehland
Thanks for the reply. I tried this a while ago - creating new pythonengine objects. But when this happened on different threads at the same time it would crash. I think it was an ironpython issue. I think that was the reason I started using the singleton. Thanks for compiledcode suggestion.
Klerk