如何在.net核心进程中捕获python sys.exit(0)?

如何在.net核心进程中捕获python sys.exit(0)?,python,.net,process,Python,.net,Process,我想使用.net核心进程运行python脚本。python代码如下所示。 在这个python脚本中,将有sys.exit(0),我希望在python scritp下面提到的.net核心代码中捕捉到这一点 import sys from pprint import pprint def main() -> None: vars = {'customer_name': sys.argv[1], 'department_name': sys.argv[2],

我想使用.net核心进程运行python脚本。python代码如下所示。 在这个python脚本中,将有sys.exit(0),我希望在python scritp下面提到的.net核心代码中捕捉到这一点

import sys
from pprint import pprint

def main() -> None:

    vars = {'customer_name': sys.argv[1],
            'department_name': sys.argv[2],
            'project_name': sys.argv[3]}
    pprint(vars)

    #raise Exception("This is an exception")
    sys.exit(1)
if __name__ == "__main__":
    main()
我想检查系统出口(0)是否为零

            try
            {
                string ConfigurationSectionName = "PythonEnv";
                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = this.configurationService.ServiceConfiguration<string>("PythonPath", ConfigurationSectionName);
                start.Arguments = this.configurationService.ServiceConfiguration<string>("LaunchScript", ConfigurationSectionName) + " " +
                                    model.CustomerName + " " +
                                    model.DepartmentName + " ";
                start.WorkingDirectory = this.configurationService.ServiceConfiguration<string>("WorkingDir", ConfigurationSectionName);
                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")
                    }
               }
            } catch(Exeception ex) {}
试试看
{
string ConfigurationSectionName=“PythonEnv”;
ProcessStartInfo start=新的ProcessStartInfo();
start.FileName=this.configurationService.ServiceConfiguration(“PythonPath”,ConfigurationSectionName);
start.Arguments=this.configurationService.ServiceConfiguration(“LaunchScript”,ConfigurationSectionName)+”+
model.CustomerName+“”+
model.DepartmentName+“”;
start.WorkingDirectory=this.configurationService.ServiceConfiguration(“WorkingDir”,ConfigurationSectionName);
start.UseShellExecute=false;//不要使用OS shell
start.CreateNoWindow=true;//我们不需要新窗口
start.RedirectStandardOutput=true;//应用程序生成的任何输出都将重定向回
start.RedirectStandardError=true;//标准输出中的任何错误都将被重定向回(例如异常)
使用(Process进程=Process.Start(Start))
{
使用(StreamReader=process.StandardOutput)
{
string stderr=process.StandardError.ReadToEnd();//以下是Python脚本的例外情况
string result=reader.ReadToEnd();//这是StdOut的结果(例如:打印“test”)
}
}
}捕获(例外情况){}

上面是.net核心代码。如何获取退出代码?

如果您只需要流程的退出代码,或许可以使用。但是如果你问“我如何区分一个正常结束的Python程序和一个通过调用
sys.exit(0)
结束的Python程序?它们有相同的退出代码”,那么我不确定有没有一个简单的解决方案。嗨@Kevin,我想知道Python是用0还是其他退出代码退出的。不是process.ExitCode(),似乎process.ExitCode不等于sys.exit(0)