子进程超时不起作用

时间:2016-05-20 20:14:44

标签: python subprocess ocaml

我在字符串中有一些错误的OCaml程序,我使用Python子进程来检查编译器的反馈。但是,因为有些程序包含无限循环,所以即使我设置了超时字段,子进程也会卡住。

环境:Windows 10,Python 3.5.1

这是我尝试过的代码:

annotated_prog = "let rec ww : ('a -> 'a * bool) * 'a -> 'a = fun (f,b)  ->  let (b',c') = f b in if c' then ww (f, b') else b';;\n let _ = let f x = let xx = (x * x) * x in (xx, (xx < 100)) in ww (f, 1);;"

try:
error_output = subprocess.run(["ocaml"], input = annotated_prog, 
                         stdout=subprocess.PIPE,universal_newlines = True, timeout=1)

except TimeoutExpired:
  error_output = None

然而,代码仍然无限循环,无法工作。

我尝试过的另一种方式:

def run(cmd, timeout_sec):
  proc = subprocess.Popen(["ocaml"], stdout=subprocess.PIPE, 
         stderr=subprocess.PIPE,universal_newlines = True)
  proc.communicate(timeout = timeout_sec)       
  print(proc.communicate("let a = b;;\n")[0])

这甚至没有给出正确的输出:

我的期望:字符8-9:错误:未绑定的值b

我得到的:OCaml版本4.02.3

这样我怎样才能从python中获得正确的输出?

def run(timeout_sec):
  proc = subprocess.Popen(["python"], stdout=subprocess.PIPE, 
  stderr=subprocess.PIPE,universal_newlines = True)
  print(cmd)
  print(proc.communicate("while True: print('hello')\n", timeout = timeout_sec)[0])

try:
      error_output =run(1)
except subprocess.TimeoutExpired:
      print('timeout')

print('error_output')

我的期望:&#39;超时&#39;或者没有一堆hellos 我得到了什么:没有

0 个答案:

没有答案
相关问题