Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何确定通过Python运行的程序(理想情况下是带有子进程)是崩溃还是成功结束_Python_Subprocess - Fatal编程技术网

如何确定通过Python运行的程序(理想情况下是带有子进程)是崩溃还是成功结束

如何确定通过Python运行的程序(理想情况下是带有子进程)是崩溃还是成功结束,python,subprocess,Python,Subprocess,我正在尝试使用Python测试一些程序。我想看看给定的某些输入是否会崩溃、结束时是否没有错误,或者运行的时间是否超过超时时间 理想情况下,我希望使用我熟悉的子流程。然而,我能够使用任何其他有用的库。我假设读取核心转储通知是一种选择,但我不知道如何做,也不知道这是否是最有效的方法。使用os,解决方案可能是: status = os.system(cmd) # status is a 16 bit number, which first 8 bits from left(lsb) talks abo

我正在尝试使用Python测试一些程序。我想看看给定的某些输入是否会崩溃、结束时是否没有错误,或者运行的时间是否超过超时时间

理想情况下,我希望使用我熟悉的子流程。然而,我能够使用任何其他有用的库。我假设读取核心转储通知是一种选择,但我不知道如何做,也不知道这是否是最有效的方法。

使用
os
,解决方案可能是:

status = os.system(cmd)
# status is a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.
sig, ret = os.WIFSIGNALED(status), os.WEXITSTATUS(status)
# then check some usual problems:
if sig:
    if status == 11:      # SIGSEGV
        print ('crashed by segfault')
    elif status == 6 :    # SIGABRT
        print('was aborted')
    else: # 14, 9 are related to timeouts if you like them
        print('was stopped abnormally with', status)
else:
    print('program finished properly')
我还没有检查子流程是否返回相同的状态。

使用
os
,解决方案可能是:

status = os.system(cmd)
# status is a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.
sig, ret = os.WIFSIGNALED(status), os.WEXITSTATUS(status)
# then check some usual problems:
if sig:
    if status == 11:      # SIGSEGV
        print ('crashed by segfault')
    elif status == 6 :    # SIGABRT
        print('was aborted')
    else: # 14, 9 are related to timeouts if you like them
        print('was stopped abnormally with', status)
else:
    print('program finished properly')


我还没有检查子流程是否返回相同的状态。

你能分享你的代码吗?我不知道如何回答你的请求。我不知道该尝试什么代码。我阅读了子流程手册,但没有找到任何方法来查看程序是否以错误结束。您可以从使用子流程调用python脚本开始,并引发错误/退出被调用的python脚本。您可以使用日志记录、标志或任何通知包来获取正在运行的脚本的状态。您可能需要查看和。您可以在一个单独的线程中启动一个子进程并超时。一旦完成,检查退出代码是非常简单的。你能分享你的代码吗?我不知道如何回答你的请求。我不知道该尝试什么代码。我阅读了子流程手册,但没有找到任何方法来查看程序是否以错误结束。您可以从使用子流程调用python脚本开始,并引发错误/退出被调用的python脚本。您可以使用日志记录、标志或任何通知包来获取正在运行的脚本的状态。您可能需要查看和。您可以在一个单独的线程中启动一个子进程并超时。一旦完成,检查退出代码非常简单。谢谢你的回答。它似乎符合我的要求。出于某种原因,当运行以SIGSEV结尾的程序时,我得到状态139。知道为什么吗?以SIGFPE结尾的程序会导致状态136。我猜有一个偏移量偏移量是128,这是程序被信号停止时的约定(linux或bash约定我不记得了,可能在某处解释过)。我必须过度简化我的代码片段…在进一步测试后,我意识到如果程序以异常结束,我可以通过使用此处的表来判断它是哪一个:但是,如果程序完成时没有异常,但返回了非零值,那我就说不出这和一个崩溃的程序有什么区别了。谢谢你的回答。它似乎符合我的要求。出于某种原因,当运行以SIGSEV结尾的程序时,我得到状态139。知道为什么吗?以SIGFPE结尾的程序会导致状态136。我猜有一个偏移量偏移量是128,这是程序被信号停止时的约定(linux或bash约定我不记得了,可能在某处解释过)。我一定是过度简化了我的代码片段……在进一步测试后,我意识到如果程序以异常结束,我可以通过使用这里的表来判断它是哪一个:然而,如果程序在没有异常的情况下结束,但返回了一个非零值,那么我无法区分它与崩溃的程序之间的区别。