BTW, PyObject po = PythonEngine.RunString(strCommand); returns 'null' when running "os.getcwd()", and PythonEngine.RunSimpleString(strCommand) returns '-1'. Currently, I'm working on Async ways to execute a python command (some commands take long to finish a task such as switch configuration, once executing a command, and returns, then gets a result later). Would you give me some suggestions how/what would be best idea for Async wrapper using Python.RunTime? Thanks, Spark. On Thu, Mar 28, 2013 at 2:35 PM, Seungweon Park <swpark71@gmail.com> wrote:
Hi,
I wrote a code PythonWrapper and SimplePythonTest which uses PythonWrapper.
using SimplePython;
namespace SimplePythonTest { /// <summary> /// Python Wrapper Tests /// </summary> [TestFixture] public class PythonTests { PythonWrapper py; /// <summary> /// PythonTests Constructor /// </summary> public PythonTests() { py = new PythonWrapper();
py.Init();
PyObject po; po = py.LoadModule("os"); po = py.LoadModule("sys"); po = py.LoadModule("psutil"); }
/// <summary> /// Python Simple Test /// </summary> [Test] public void CommonPythonTests() { PyObject po = py.Execute("os.getcwd()"); Trace.WriteLine(po.Repr());
py.Close(); } } }
namespace SimplePython { /// <summary> /// Python Wrapper via XMLRPC/Python 2.7.3/.Net for Python. /// </summary> public class PythonWrapper { /// <summary> /// Inter Lock /// </summary> private IntPtr gs;
/// <summary> /// Constructor /// </summary> public PythonWrapper() { }
/// <summary> /// Initialize PythonEngine /// </summary> public void Init() { PythonEngine.Initialize(); gs = PythonEngine.AcquireLock();
}
/// <summary> /// Load Module /// </summary> /// <param name="strModule">Module Name</param> /// <returns></returns> public PyObject LoadModule(string strModule) { return PythonEngine.ImportModule(strModule); }
/// <summary> /// Execute Python Command /// </summary> /// <param name="strCommand">Python Command</param> /// <returns></returns> public PyObject Execute(string strCommand) { PyObject po = PythonEngine.RunString(strCommand);
return po; }
public void Close() { PythonEngine.ReleaseLock(gs); PythonEngine.Shutdown(); } } }
When testing (CommonPythonTests), it initializes and loads modules, but PythonEngine.RunString() doesn't seem to work properly. Is any something wrong in my code which gets os.getcwd()? Furthermore, I can't find the difference between PythonEngine.RunString() and PythonEngine.RunSimpleString().
Thank you, Spark.