如何从我的 dotnet 核心 windows 服务获取我的 Python 脚本到 运行?
How can I get my Python script to run from my dotnet core windows service?
一些背景,
我有一个 dotnet windows 服务 需要来自 Python 脚本的一些数据 (main.py)
main.py,它使用多个模块,包括 numpy、scipy 和其他第 3 方模块,
负责做一些目前C#做不了的逻辑计算
main.py 接受少量参数和 return 一些结果,然后我们的 dotnet windows服务.
我正在使用 Python v3.8
现在,我正在努力想出一个让 dotnet windows 服务和 python 脚本一起工作的解决方案。有使用 IronPython 的建议,但我相信它对导入第 3 方模块有限制,这在这种情况下可能不起作用。
我正在考虑将此 main.py 用作 微服务 即 restful API 使用 Flask 可能通过这样做我的 dotnet windows 服务 将可以在需要 python 脚本执行计算时发出请求,并且它是可扩展的开始在其他地方使用它
否则,如果您认为可以采用不同的方式,请提出建议
我很乐意并欢迎任何建议、建议或问题。
像这样添加一些东西:
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);
Ironpython 与 Python 2.7 兼容。 Python3 支持还有很长的路要走。如果您使用的是 numpy 和 scipy,我肯定会选择微服务路线,这样您就可以使用当前受支持的版本。
现在您的 C# 正在调用 REST API python 服务是否需要 运行 on Windows?你能在 linux 盒子上 运行 或 Windows 运行ning WSL 吗?
一些背景,
我有一个 dotnet windows 服务 需要来自 Python 脚本的一些数据 (main.py)
main.py,它使用多个模块,包括 numpy、scipy 和其他第 3 方模块, 负责做一些目前C#做不了的逻辑计算
main.py 接受少量参数和 return 一些结果,然后我们的 dotnet windows服务.
我正在使用 Python v3.8
现在,我正在努力想出一个让 dotnet windows 服务和 python 脚本一起工作的解决方案。有使用 IronPython 的建议,但我相信它对导入第 3 方模块有限制,这在这种情况下可能不起作用。
我正在考虑将此 main.py 用作 微服务 即 restful API 使用 Flask 可能通过这样做我的 dotnet windows 服务 将可以在需要 python 脚本执行计算时发出请求,并且它是可扩展的开始在其他地方使用它
否则,如果您认为可以采用不同的方式,请提出建议
我很乐意并欢迎任何建议、建议或问题。
像这样添加一些东西:
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);
Ironpython 与 Python 2.7 兼容。 Python3 支持还有很长的路要走。如果您使用的是 numpy 和 scipy,我肯定会选择微服务路线,这样您就可以使用当前受支持的版本。
现在您的 C# 正在调用 REST API python 服务是否需要 运行 on Windows?你能在 linux 盒子上 运行 或 Windows 运行ning WSL 吗?