Python3: 超出资源限制时出现异常或 return 代码?
Python3: Exception or return code when resource limit exceeded?
我是运行宁python3.4.3。我有一个子进程可能 运行 很长时间,或者生成的文件太大。我有这个代码。
def setlimits():
if DEBUG:
print("Setting resource limit in child (pid {0})".format(os.getpid()))
resource.setrlimit(resource.RLIMIT_CPU, (.05, .05)) # CPU time in secs
resource.setrlimit(resource.RLIMIT_FSIZE, (1000000, 1000000)) # file size
resource.setrlimit(resource.RLIMIT_NOFILE, (20, 20)) # Number open files
然后我用这段代码调用子进程(在一个更大的例程中)。
rc = None
try:
rc = subprocess.call(["potentially long-running command"],shell=True, preexec_fn=setlimits)
except Exception as err:
print("Exception happened")
rc = -1
print("rc = {0}".format(str(rc)))
当我向它提供一个 运行 长的进程时,它不会给我异常。 (我预计 OSError 基于资源文档。)它给了我一个 rc=137.
知道这是在哪里记录的吗?我当然想知道我已经涵盖了所有案例;我需要 if rc>128
类型的支票吗?
137 是一个 reserved exit code 表示进程已被终止 - 它等于 128 + 9(其中 9 代表信号 9 或 SIGKILL)。这很可能发生在进程达到其硬 CPU 限制并被内核杀死时。
subprocess.call()
does not raise an exception if the called process returns with a nonzero exit status. If you want that behaviour, you're better off with either subprocess.check_call()
or subprocess.run(..., check=True)
, which will raise CalledProcessError
非零退出状态。
我是运行宁python3.4.3。我有一个子进程可能 运行 很长时间,或者生成的文件太大。我有这个代码。
def setlimits():
if DEBUG:
print("Setting resource limit in child (pid {0})".format(os.getpid()))
resource.setrlimit(resource.RLIMIT_CPU, (.05, .05)) # CPU time in secs
resource.setrlimit(resource.RLIMIT_FSIZE, (1000000, 1000000)) # file size
resource.setrlimit(resource.RLIMIT_NOFILE, (20, 20)) # Number open files
然后我用这段代码调用子进程(在一个更大的例程中)。
rc = None
try:
rc = subprocess.call(["potentially long-running command"],shell=True, preexec_fn=setlimits)
except Exception as err:
print("Exception happened")
rc = -1
print("rc = {0}".format(str(rc)))
当我向它提供一个 运行 长的进程时,它不会给我异常。 (我预计 OSError 基于资源文档。)它给了我一个 rc=137.
知道这是在哪里记录的吗?我当然想知道我已经涵盖了所有案例;我需要 if rc>128
类型的支票吗?
137 是一个 reserved exit code 表示进程已被终止 - 它等于 128 + 9(其中 9 代表信号 9 或 SIGKILL)。这很可能发生在进程达到其硬 CPU 限制并被内核杀死时。
subprocess.call()
does not raise an exception if the called process returns with a nonzero exit status. If you want that behaviour, you're better off with either subprocess.check_call()
or subprocess.run(..., check=True)
, which will raise CalledProcessError
非零退出状态。