Anonymous avatar Anonymous committed f72abea

Now uses a flat process chain for async_sub.py, no intermediary shell. Uses TerminateProcess for process control.

Comments (0)

Files changed (1)

 import tempfile
 
 if subprocess.mswindows:
-    # sys.path.append('async_libs.zip')
     from win32file import ReadFile, WriteFile
     from win32pipe import PeekNamedPipe
-
+    import win32api
     import msvcrt
 
-    def _call_proc(cmd):
-        return subprocess.Popen (
-            cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = 1,
-        )
-
-    win32_kill_commands = (
-        ('pskill', 'pskill -t %s'),
-        ('taskkill /?', 'taskkill /F /T /PID %s'),  # /? so no err code
-    )
-
-    for test_cmd, kill_cmd in win32_kill_commands:
-        if _call_proc(test_cmd).wait() is not 1:
-            os.kill = lambda pid: _call_proc(kill_cmd % pid)
-            break
-
-        else: os.kill = None
-
-    if os.kill is None:
-        raise SystemExit('No way of killing unruly processes. Try installing '
-                         'sysinternals pskill and placing on %PATH%.')
-
 else:
     import select
     import fcntl
         setattr(self, which, None)
     
     if subprocess.mswindows:
+        def kill(self):
+            """kill function for Win32"""
+            handle = win32api.OpenProcess(1, 0, self.pid)
+            return (0 != win32api.TerminateProcess(handle, 0))
+        
         def send(self, input):
             if not self.stdin:
                 return None
             return read
 
     else:
+        def kill(self):
+            os.kill(self.pid)
+        
         def send(self, input):
             if not self.stdin:
                 return None
 
 def proc_in_time_or_kill(cmd, time_out):
     proc = Popen (
-        cmd, shell = True, bufsize = -1,
+        cmd, bufsize = -1,
         stdin = subprocess.PIPE, stdout = subprocess.PIPE, 
         stderr = subprocess.STDOUT, universal_newlines = 1
     )
         response += [proc.read_async(wait=0.1, e=0)]
 
     if ret_code is None:
-        os.kill(proc.pid)
+        proc.kill()
         ret_code = '"Process timed out (time_out = %s secs)"' % time_out
 
     return ret_code, ''.join(response)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.