Просмотр исходного кода

Prevent too many files from being opened; prevent zombie process

Yentl Van Tendeloo 8 лет назад
Родитель
Сommit
ca0fd1b12a
2 измененных файлов с 14 добавлено и 7 удалено
  1. 6 3
      integration/utils.py
  2. 8 4
      interface/HUTN/hutn_compiler/compiler.py

+ 6 - 3
integration/utils.py

@@ -55,15 +55,18 @@ def execute(scriptname, parameters=[], wait=False):
 
 def child_kill(pid):
     subprocess.call(["pkill", "-P", "%i" % pid])
-    while subprocess.call(["pgrep", "-P", "%i" % pid], stdout=open(os.devnull, 'w')) != 1:
-        time.sleep(0.1)
+    with open(os.devnull, 'w') as null:
+        while subprocess.call(["pgrep", "-P", "%i" % pid], stdout=null) != 1:
+            time.sleep(0.1)
 
 def kill(process):
     if os.name == "nt":
+        #TODO this is Windows and I don't care too much about that...
         subprocess.call(["taskkill", "/F", "/T", "/PID", "%i" % process.pid])
     elif os.name == "posix":
         child_kill(process.pid)
-        process.poll()
+        process.terminate()
+        process.wait()
         child_kill(os.getpid())
 
 def flush_data(address, data):

+ 8 - 4
interface/HUTN/hutn_compiler/compiler.py

@@ -23,7 +23,8 @@ def do_parse(inputfile, grammarfile):
         try:
             if os.path.getmtime(picklefile) > os.path.getmtime(grammarfile):
                 # Pickle is more recent than grammarfile, so use it
-                grammar = pickle.load(open(picklefile, 'rb'))
+                with open(picklefile, 'rb') as f:
+                    grammar = pickle.load(f)
                 new_grammar = False
             else:
                 # Will be catched immediately
@@ -40,7 +41,8 @@ def do_parse(inputfile, grammarfile):
             grammar = Grammar()          
             grammar.rules = structure['rules']
             grammar.tokens = structure['tokens']        
-            pickle.dump(grammar, open(picklefile, 'wb'), pickle.HIGHEST_PROTOCOL)
+            with open(picklefile, 'wb') as f:
+                pickle.dump(grammar, f, pickle.HIGHEST_PROTOCOL)
         parsers[grammarfile] = grammar
     else:
         new_grammar = False
@@ -50,7 +52,8 @@ def do_parse(inputfile, grammarfile):
     try:
         if os.path.getmtime(picklefile) > os.path.getmtime(inputfile):
             # Pickle is more recent than inputfile, so use it
-            result = pickle.load(open(picklefile, 'rb'))
+            with open(picklefile, 'rb') as f:
+                result = pickle.load(f)
         else:
             # Inputfile has changed
             raise Exception()
@@ -59,7 +62,8 @@ def do_parse(inputfile, grammarfile):
         if result['status'] != Parser.Constants.Success:
             msg = "%s:%s:%s: %s" % (inputfile, result["line"], result["column"], result["text"])
             raise Exception(msg)
-        pickle.dump(result, open(picklefile, 'wb'), pickle.HIGHEST_PROTOCOL)
+        with open(picklefile, 'wb') as f:
+            pickle.dump(result, f, pickle.HIGHEST_PROTOCOL)
 
     return result