[Python.NET] How to run a command by Embedded Python?

brad at fie.us brad at fie.us
Thu Mar 28 23:38:24 CET 2013


Doesn't it just depend on where you want the async to be?  On the python side or on the .net side?  You could do it all in the python side and just stick to python async methodologies.  Or you could do it in .net in which case you need to use the multi-threaded aspects of pythonnet.

-brad

On Mar 28, 2013, at 6:22 PM, Seungweon Park <swpark71 at gmail.com> wrote:

> 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 at 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.
> 
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20130328/e0e39b06/attachment.html>


More information about the PythonDotNet mailing list