Python 子进程异常处理
Python subprocess exception handling
我有一个用C++编写的程序,它所做的只是除以 1/0 以产生异常。
我通过 Python 的子进程库运行该程序的.exe。我的目标是捕获并记录程序在 Python 中C++异常。
p = subprocess.Popen(['C:\Users\name\Desktop\Win32Project1.exe'])
当这行代码执行时,p 是一个非零值,这意味着发生了错误。我正在运行Windows 7并使用Python 3.4.1
我很
确定您无法跟踪来自不同进程的异常。您应该尝试在C++程序中捕获异常,并通过许多进程间通信方式之一传递它。
呵呵,卡比
试试这个
cmd = ['C:\Users\name\Desktop\Win32Project1.exe']
out, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
print out # this prints the output of the execution
print err # this prints if any error is returned
我没有窗口,所以这没有经过测试,但可能会对您有所帮助,有关更多信息,请查看此处
您还可以将其他参数传递给子进程。Popen 指定 stdin 和 stderr 去哪里。
然后你可以这样说:
p = subprocess.Popen('C:\Users\name\Desktop\Win32Project1.exe', stdout=PIPE, stderr=PIPE)
(stdout_data, stderr_data) = p.communicate()
rc = p.returncode
# assume 0 return code means o.k.
if rc:
parse_error(stderr_data)
# end if
相关文章:
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- 终止 QProcess 不会终止子进程
- 什么时候最好在子进程中使用 CPU 或 I/O 密集型代码 [ C++ ]
- 子进程更新共享 mmap 内存,但父进程没有更改
- Firebase C++VS2018 SDL2-在Firebase::app::create(..)上执行异常处理
- 使用 stoi 功能进行异常处理
- 子系统中的异常处理:本机
- 使用 waitpid 时等待子进程终止
- 使用重定向标准处理子进程中的 kbhit
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 与异常处理程序中的操作员<<不匹配
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- kill() 总是返回 0(成功),即使在子进程已经结束之后?
- 数组 C++ 上的异常处理程序
- 父进程和子进程之间的 POSIX 信号量
- 检测到由于操作系统内存不足而导致子进程终止
- 在 Bash 脚本中处理来自子进程的信号
- 子进程 c++ 的 Windows 控制台信号处理
- Python 子进程异常处理