【问题标题】:How can I get my Python script to run from my dotnet core windows service?如何让我的 Python 脚本从我的 dotnet core windows 服务运行?
【发布时间】:2020-08-23 08:02:19
【问题描述】:

一点背景,

  • 我有一个 dotnet windows 服务,它需要来自 Python 脚本 (main.py) 的一些数据

  • ma​​in.py,它使用了多个模块,包括numpy、scipy等第三方模块, 负责做一些目前C#无法完成的逻辑计算

  • ma​​in.py 接受少量参数并返回一些结果,然后由我们的 dotnet windows 服务使用。

  • 我使用的是 Python v3.8

现在,我正在努力想出一个让 dotnet windows 服务和 python 脚本一起工作的解决方案。有人建议使用 IronPython,但我认为它在导入 3rd 方模块方面存在限制,在这种情况下可能无法正常工作。

我正在考虑将这个 ma​​in.py 用作 微服务,即使用 Flask 的宁静 API,可能通过这样做我的 dotnet windows 服务 will 可以在需要 python 脚本执行计算时发出请求,并且在其他地方使用它时具有可扩展性

否则,如果您认为可以采取不同的方式,请提出建议

我很乐意并欢迎任何建议、建议或问题。

【问题讨论】:

    标签: python c# .net-core microservices ironpython


    【解决方案1】:

    Ironpython 与 Python 2.7 兼容。 Python3 支持还有很长的路要走。如果您使用的是 numpy 和 scipy,我肯定会选择微服务路线,以便您可以使用当前受支持的版本。

    既然您的 C# 正在调用 REST API,python 服务是否需要在 Windows 上运行?您可以在运行 WSL 的 linux 机器或 Windows 上运行吗?

    【讨论】:

    • 我还没有研究影响/限制。就目前而言,建议的微服务将在 Windows 上运行,与上面提到的 dotnet windows 服务相同。有什么我需要注意的吗?谢谢@WombatPM
    • 如果你不在生产环境中,我会安装 Apache 或 NGINX 并避免使用 IIS 来运行微服务。
    • 如果我在生产中?在这种情况下你有什么建议?
    • 这取决于运营团队,假设您有一个。具有标准监控、日志记录和报告的 Ops 团队通常希望 Widows 机器上的服务在 IIS 下运行。如果您没有这样的要求,那么您可以随心所欲。多年来,我使用 Apache 和桌面设备运行了一个仅限内部的 API。
    【解决方案2】:

    添加类似这个方法的东西:

         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;
                        }
                    }
                }
    
    }
    

    然后,像下面这样调用你的 python 文件:

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

    【讨论】:

    • 参考上面的示例并假设 your_python_file.py 中的脚本需要一些 3rd 方模块才能运行。如何导入 3rd 方模块? your_python_file.py 是否应该在特定环境中运行?我原以为应该在运行 your_python_file.py 之前预先安装这些模块。谢谢阿布杜斯
    猜你喜欢
    • 2017-04-06
    • 2017-04-15
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    相关资源
    最近更新 更多