为什么没有ulimit -t杀死一个超出限制的进程time.sleep?

时间:2016-08-20 18:12:49

标签: python ulimit

我试图使用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

脚本一直在运行。这有什么解释吗?感谢。

mata的答案是正确的。我更新了我的实际代码,其中包含ulimit -t不计算生成的子进程的运行时间的隐藏事物。

#!/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)

1 个答案:

答案 0 :(得分:3)

ulimit -t设置 CPU 时间限制。当您的程序处于休眠状态时,它不会占用任何CPU时间,因此时间不计算在内。它只会占用几个CPU周期才能重新入睡,这就是它没有被杀死的原因。

您无法使用ulimit指定实时限制。