为什么 ulimit -t 不杀死超过 time.sleep 限制的进程?
Why doesn't ulimit -t kill a process sleeping beyond the limit with time.sleep?
我正在尝试使用 ulimit -t
限制一个 python 脚本的 CPU 使用。脚本包含一条time.sleep()
语句,在指定的时间限制后不被杀死。这是名为 test.py
:
的简化 python 脚本
import time
while True:
time.sleep(0.1)
我运行命令如下:
ulimit -v 400000; ulimit -t 30; python test.py
脚本永远保持 运行ning。对此有什么解释吗?谢谢。
mata的回答是正确的。我更新了我的真实代码,其中包含一个隐藏的东西,ulimit -t
不计算生成子进程的 运行ning 时间。
#!/usr/bin/env python
# Run: python smt.py filename.smt2 timeout
# timeout is in seconds
import os
import subprocess
import sys
import stat
import time
current_path = os.path.dirname(os.path.realpath(__file__))
def remove_tmp (filename, version):
try:
os.remove(filename + '.' + version + '.tmp')
except OSError:
pass
try:
os.remove(os.path.splitext(filename)[0] + '.' + version + '.out')
except OSError:
pass
try:
os.remove(os.path.splitext(filename)[0] + '.' + version + '.in')
except OSError:
pass
def run_raSAT (filename, bounds, sbox, timeout):
startTime = time.time()
raSATResult = "unknown"
# remove tmps files:
remove_tmp(filename, "0.2")
remove_tmp(filename, "0.3")
proc2 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.2"), filename, bounds, 'sbox=' + str(sbox), 'tout=' + str(timeout-(time.time() - startTime))])
proc3 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.3"), filename, bounds])
while True:
if proc2.poll():
# try read output of 0.2
try:
with open(filename + '.0.2.tmp', 'r') as outfile:
raSATResult = outfile.read().rstrip()
outfile.close()
if raSATResult == "unknown":
sbox /= 10
remove_tmp(filename, "0.2")
proc2 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.2"), filename, bounds, 'sbox=' + str(sbox), 'tout=' + str(timeout-(time.time() - startTime))])
except IOError:
pass
if proc3.poll():
# try read output of 0.3
try:
with open(filename + '.0.3.tmp', 'r') as outfile:
raSATResult = outfile.read().rstrip()
outfile.close()
except IOError:
pass
if raSATResult == "sat" or raSATResult == "unsat":
if not proc3.poll():
proc3.kill()
if not proc2.poll():
proc2.kill()
break
time.sleep(0.01)
return raSATResult, sbox
def run(filename, initLowerBound, initUpperBound, sbox, timeout):
lowerBound = initLowerBound
upperBound = initUpperBound
raSATResult = "unknown"
startTime = time.time()
while (raSATResult == 'unknown'):
(raSATResult, sbox) = run_raSAT(filename, 'lb=' + str(lowerBound) + ' ' + str(upperBound), sbox, timeout - (time.time() - startTime))
if raSATResult == 'unsat':
(raSATResult, sbox) = run_raSAT(filename, 'lb=-inf inf', sbox, timeout - (time.time() - startTime))
print (raSATResult)
# remove tmps files:
remove_tmp(filename, "0.2")
remove_tmp(filename, "0.3")
# get timeout from environment
timeout = float(os.environ.get('STAREXEC_CPU_LIMIT'))
run(sys.argv[1], -10, 10, 0.1, timeout)
ulimit -t
设置 CPU 时间限制。当您的程序处于休眠状态时,它不会使用任何 CPU 时间,因此该时间不算数。它只会占用几个 CPU 周期再次进入休眠状态,这就是它没有被杀死的原因。
您不能使用 ulimit 指定实时限制。
我正在尝试使用 ulimit -t
限制一个 python 脚本的 CPU 使用。脚本包含一条time.sleep()
语句,在指定的时间限制后不被杀死。这是名为 test.py
:
import time
while True:
time.sleep(0.1)
我运行命令如下:
ulimit -v 400000; ulimit -t 30; python test.py
脚本永远保持 运行ning。对此有什么解释吗?谢谢。
mata的回答是正确的。我更新了我的真实代码,其中包含一个隐藏的东西,ulimit -t
不计算生成子进程的 运行ning 时间。
#!/usr/bin/env python
# Run: python smt.py filename.smt2 timeout
# timeout is in seconds
import os
import subprocess
import sys
import stat
import time
current_path = os.path.dirname(os.path.realpath(__file__))
def remove_tmp (filename, version):
try:
os.remove(filename + '.' + version + '.tmp')
except OSError:
pass
try:
os.remove(os.path.splitext(filename)[0] + '.' + version + '.out')
except OSError:
pass
try:
os.remove(os.path.splitext(filename)[0] + '.' + version + '.in')
except OSError:
pass
def run_raSAT (filename, bounds, sbox, timeout):
startTime = time.time()
raSATResult = "unknown"
# remove tmps files:
remove_tmp(filename, "0.2")
remove_tmp(filename, "0.3")
proc2 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.2"), filename, bounds, 'sbox=' + str(sbox), 'tout=' + str(timeout-(time.time() - startTime))])
proc3 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.3"), filename, bounds])
while True:
if proc2.poll():
# try read output of 0.2
try:
with open(filename + '.0.2.tmp', 'r') as outfile:
raSATResult = outfile.read().rstrip()
outfile.close()
if raSATResult == "unknown":
sbox /= 10
remove_tmp(filename, "0.2")
proc2 = subprocess.Popen([os.path.join(current_path, "./raSAT-0.2"), filename, bounds, 'sbox=' + str(sbox), 'tout=' + str(timeout-(time.time() - startTime))])
except IOError:
pass
if proc3.poll():
# try read output of 0.3
try:
with open(filename + '.0.3.tmp', 'r') as outfile:
raSATResult = outfile.read().rstrip()
outfile.close()
except IOError:
pass
if raSATResult == "sat" or raSATResult == "unsat":
if not proc3.poll():
proc3.kill()
if not proc2.poll():
proc2.kill()
break
time.sleep(0.01)
return raSATResult, sbox
def run(filename, initLowerBound, initUpperBound, sbox, timeout):
lowerBound = initLowerBound
upperBound = initUpperBound
raSATResult = "unknown"
startTime = time.time()
while (raSATResult == 'unknown'):
(raSATResult, sbox) = run_raSAT(filename, 'lb=' + str(lowerBound) + ' ' + str(upperBound), sbox, timeout - (time.time() - startTime))
if raSATResult == 'unsat':
(raSATResult, sbox) = run_raSAT(filename, 'lb=-inf inf', sbox, timeout - (time.time() - startTime))
print (raSATResult)
# remove tmps files:
remove_tmp(filename, "0.2")
remove_tmp(filename, "0.3")
# get timeout from environment
timeout = float(os.environ.get('STAREXEC_CPU_LIMIT'))
run(sys.argv[1], -10, 10, 0.1, timeout)
ulimit -t
设置 CPU 时间限制。当您的程序处于休眠状态时,它不会使用任何 CPU 时间,因此该时间不算数。它只会占用几个 CPU 周期再次进入休眠状态,这就是它没有被杀死的原因。
您不能使用 ulimit 指定实时限制。