以下均为Linux环境测试。

起因:

开发的一个程序,经常会由于内存不足而被kill掉,使用的是os.system函数执行的,返回值总是35072,当时没多想。后来由于一些原因,要模拟OOM 被kill的状态,于是调用 sys.exit(35072) 突然发现,返回值不对。。。 于是有了下面的。

python中执行系统命令的方式一般来说有两种,os和subprocess(推荐)。

简单用法如下:

import os
os.system("ls")
# or
import subprocess
subprocess.call(["ls"])

本文重点在于此两种方式的返回值差异问题。

一、os.system

此函数返回值即为 os.wait 函数返回值中的 exit status,查看官方文档对 os.wait 的解释:

Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced

os.wait 返回一个元组,包含进程id和进程退出状态码。

敲重点:

进程退出码由两部分组成,分别是进程被kill的信号值和进程的退出码。

这两个数字合并为一个16位的数字,前8位是退出码,后8位是kill信号。

10进制计算方式:

进程退出码 = 进程退出码 * 2 ** 8 + kill信号值

测试:

python中可以调用sys.exit或者os._exit来模拟各种退出码

import os

cmd = "python -c 'import sys;sys.exit(1)'"
r = os.system(cmd)
print(r) # r = 256

由于没有被kill,故kill信号值为0,所以 r = 1 * 2 ** 8 + 0 = 256

当进程被kill时,则没有退出码,故 r = kill信号值

tips:

使用 os.WEXITSTATUS 可将 os.system 返回值还原为退出码

二、subprocess.call

此函数返回的是subprocess.Popen对象的returncode属性,这个没找到文档,直接看源码吧。

def _handle_exitstatus(
self,
sts,
_WIFSIGNALED=os.WIFSIGNALED,
_WTERMSIG=os.WTERMSIG,
_WIFEXITED=os.WIFEXITED,
_WEXITSTATUS=os.WEXITSTATUS,
_WIFSTOPPED=os.WIFSTOPPED,
_WSTOPSIG=os.WSTOPSIG,
):
"""All callers to this function MUST hold self._waitpid_lock."""
# This method is called (indirectly) by __del__, so it cannot
# refer to anything outside of its local scope.
if _WIFSIGNALED(sts):
self.returncode = -_WTERMSIG(sts)
elif _WIFEXITED(sts):
self.returncode = _WEXITSTATUS(sts)
elif _WIFSTOPPED(sts):
self.returncode = -_WSTOPSIG(sts)
else:
# Should never happen
raise SubprocessError("Unknown child exit status!")

在源码中可发现,它对不同类型的退出码(或者叫退出码不太合适。。)做了判断,如果是被kill或被停止,则返回一个负值,如果是正常退出码,则原样返回。

测试:

import subprocess

r = subprocess.call(["python", "-c", "import sys;sys.exit(1)"])
print(r) # r=1

在没有被kill时,r = 退出码 = 1

当被kill时,懒得写代码了,返回值是 -9、-15等

参见:Linux 信号signal处理机制

ps:

Linux errno 的详细说明可参见:

Linux errno详解

补充一个:

errno: 137 OOM killer

参考文献:

C语言 unsigned与signed区别

https://docs.python.org/3.7/library/subprocess.html#subprocess.Popen.wait

https://docs.python.org/3.7/library/os.html#os.system

https://shapeshed.com/unix-exit-codes/

Linux errno 与 Python的更多相关文章

  1. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  2. 【转】windows和linux中搭建python集成开发环境IDE

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  3. linux下安装python

    在Linux下安装Python的操作相当简单,按如下步骤操作即可: 命令: wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgzt ...

  4. Linux 下安装python软件包(pip、nose、virtualenv、distribute )

    新手刚开始学习Python,目前学习<笨方法学python>ing- 在学习习题46时需要安装几个软件包:pip.nose.virtualenv.distribute !在此记录Linux ...

  5. Linux环境下Python的安装过程

    Linux环境下Python的安装过程 前言 一般情况下,Linux都会预装 Python了,但是这个预装的Python版本一般都非常低,很多 Python的新特性都没有,必须重新安装新一点的版本,从 ...

  6. linux下安装python linux下一些常用的命令

    注意 ubuntukylin-14.04.2-desktop-amd64 自带python2.7.6 这个说的比较详细 http://wenku.baidu.com/link?url=gaeFcQrc ...

  7. linux环境下 python环境import找不到自定义的模块

    linux环境下 python环境import找不到自定义的模块 问题现象: Linux环境中自定义的模块swport,import swport 出错.swport模块在/root/sw/目录下. ...

  8. linux配置Anaconda python集成环境

    1.下载anaconda与安装 利用anaconda来配置python环境 如果你上面两步已经没有问题了,那么这一步可以省略. 如果你想简单一些,利用anaconda来配置python环境,那么直接从 ...

  9. 环境部署(九):linux下安装python+chrome+Xvfb

    在基于selenium进行的UI自动化测试中,开发调试环境一般都是windows操作系统.完成后需要部署到专门的测试环境. 如要要部署到linux环境的服务器(阿里云.腾讯云)执行,那么测试脚本也需要 ...

随机推荐

  1. spark基础知识三

    主要围绕spark的底层核心抽象RDD和原理进行理解.主要包括以下几个方面 RDD弹性分布式数据集的依赖关系 RDD弹性分布式数据集的lineage血统机制 RDD弹性分布式数据集的缓存机制 spar ...

  2. Debian使用小计

    1. Debian无法apt install debian安装完成后,如果运行apt install,提示 Media change: please insert the disc labeled ' ...

  3. Lab7:同步互斥

    并发进程的正确性 独立进程 不和其他进程共享资源或状态 确定性 -> 输入状态决定结果 可重现 -> 能够重现起始条件 调度顺序不重要 并发进程 在多个进程间有资源共享 不确定性 不可重现 ...

  4. rent a apartment

    今日焦点 month-to-month 按月 6-month minimum 至少六个月 sublease 转租 drenched in sunlight 阳光充足的 词汇实践 I am lookin ...

  5. TensorFlow中的 tensor 张量到底是什么意思?

    详见[Reference]: TensorFlow中的“Tensor”到底是什么? 以下摘录一些要点: 这个图好生动呀!~ 标量和向量都是张量(tensor).

  6. CUDA 到底什么玩意

    * CUDA与cuDNN * 什么是CUDA * CUDA(ComputeUnified Device Architecture),是显卡厂商NVIDIA推出的运算平台. CUDA是一种由NVIDIA ...

  7. AQS3---出队(凌乱,供参考,先看AQS2)

    出队时候,如果队列处于稳定状态,那么就是一个挨着一个出队,难点在于出队的时候,队列正处于调整阶段,那么此时队列中的关系是混乱无章可寻的.  出队:unlock释放锁,不在队列线程去抢锁,队列第一个正常 ...

  8. windows x64安装与测试redis

    说明:安装与测试的系统为windows X64: 1.下载redis:https://github.com/microsoftarchive/redis/releases 2.解压Redis-x64- ...

  9. 使用benchmarkSQL测试数据库的TPCC

    压力测试是指在MySQL上线前,需要进行大量的压力测试,从而达到交付的标准.压力测试不仅可以测试MySQL服务的稳定性,还可以测试出MySQL和系统的瓶颈. TPCC测试:Transaction Pr ...

  10. 低版本的 eclipse 不支持 tomcat8.5 的解决方法

    目录 低版本的eclipse 不支持 tomcat8.5,但是还想使用的方法 低版本的eclipse 不支持 tomcat8.5,但是还想使用的方法 1. 介绍:   我在使用 mars 版本的 ec ...