Subprocess Timeout funktioniert nicht

Ich habe einige fehlerhafte OCaml-Programme in Strings und verwende den Python-Unterprozess, um das Feedback vom Compiler zu überprüfen. Da es jedoch einige Programme gibt, die Endlosschleifen enthalten, bleibt der Unterprozess auch dann hängen, wenn ich das Zeitlimit-Feld festgelegt habe.

Umgebung: Windows 10, Python 3.5.1

Dies ist der Code, den ich ausprobiert habe:

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

Der Code geht jedoch immer noch in die Endlosschleife und kann nicht funktionieren.

Auf andere Weise habe ich versucht:

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])

Dies gibt nicht einmal eine korrekte Ausgabe:

was ich erwarte: Zeichen 8-9: Fehler: Ungebundener Wert b

was ich habe: OCaml Version 4.02.3

Wie kann ich auf diese Weise sogar die korrekte Ausgabe von Python erhalten?

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')

Was ich erwarte: "Auszeit" mit oder ohne ein paar Höllen Was ich habe: Keine

Antworten auf die Frage(0)

Ihre Antwort auf die Frage