简体   繁体   中英

How can I get my Python script to run from my dotnet core windows service?

A bit of background,

  • I have a dotnet windows service that needs some data from a Python script (main.py)

  • main.py , which uses multiple modules including numpy, scipy and other 3rd party modules, is responsible for doing some logic calculations which currently are impossible to be done in C#

  • main.py accepts few parameters and return some results which they are then consumed by our dotnet windows service .

  • I am using Python v3.8

Now, I am struggling to come up with a solution to have both dotnet windows service and python script to work together. There are suggestions to use IronPython but I believe it has limitation on importing 3rd party modules which might not work in this scenario.

I was thinking of using this main.py as a microservice ie a restful API using Flask probably by doing this my dotnet windows service will can make requests whenever it needs the python script to carry out the calculations, and it's scalable when it comes to using it elsewhere

Otherwise please advice if you believe this can be done differently

I would love and welcome any suggestions, recommendations or questions.

Ironpython is compatible with Python 2.7. Python3 support is a long way off. If you are using numpy and scipy, I'd definitely go the microservice route so that you can be using current, supported versions.

So now that your C# is calling a REST API does the python service need to run on Windows? Can you run on a linux box or Windows running WSL?

Add SOmething like this method:

     public class YourClass
        {
            public string Run(string cmd, string args)
            {
                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = "python";
                start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);
                start.UseShellExecute = false;// Do not use OS shell
                start.CreateNoWindow = true; // We don't need new window
                start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
                start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
                using (Process process = Process.Start(start))
                {
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
                        string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
                        return result;
                    }
                }
            }

}

And then, call your python file there like below:

var res = new YourClass().Run("your_python_file.py","params");
 Console.WriteLine(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM