Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 1 | import os |
| 2 | import msvcrt |
| 3 | import signal |
| 4 | import sys |
| 5 | import _winapi |
| 6 | |
| 7 | from . import spawn |
| 8 | from . import popen |
| 9 | from . import reduction |
| 10 | from . import util |
| 11 | |
| 12 | __all__ = ['Popen'] |
| 13 | |
| 14 | # |
| 15 | # |
| 16 | # |
| 17 | |
| 18 | TERMINATE = 0x10000 |
| 19 | WINEXE = (sys.platform == 'win32' and getattr(sys, 'frozen', False)) |
| 20 | WINSERVICE = sys.executable.lower().endswith("pythonservice.exe") |
| 21 | |
| 22 | # |
| 23 | # We define a Popen class similar to the one from subprocess, but |
| 24 | # whose constructor takes a process object as its argument. |
| 25 | # |
| 26 | |
| 27 | class Popen(object): |
| 28 | ''' |
| 29 | Start a subprocess to run the code of a process object |
| 30 | ''' |
| 31 | method = 'spawn' |
| 32 | |
| 33 | def __init__(self, process_obj): |
| 34 | prep_data = spawn.get_preparation_data(process_obj._name) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 35 | |
| 36 | # read end of pipe will be "stolen" by the child process |
| 37 | # -- see spawn_main() in spawn.py. |
| 38 | rhandle, whandle = _winapi.CreatePipe(None, 0) |
| 39 | wfd = msvcrt.open_osfhandle(whandle, 0) |
Richard Oudkerk | 7d2d43c | 2013-08-22 11:38:57 +0100 | [diff] [blame] | 40 | cmd = spawn.get_command_line(parent_pid=os.getpid(), |
| 41 | pipe_handle=rhandle) |
| 42 | cmd = ' '.join('"%s"' % x for x in cmd) |
Richard Oudkerk | 84ed9a6 | 2013-08-14 15:35:41 +0100 | [diff] [blame] | 43 | |
| 44 | with open(wfd, 'wb', closefd=True) as to_child: |
| 45 | # start process |
| 46 | try: |
| 47 | hp, ht, pid, tid = _winapi.CreateProcess( |
| 48 | spawn.get_executable(), cmd, |
| 49 | None, None, False, 0, None, None, None) |
| 50 | _winapi.CloseHandle(ht) |
| 51 | except: |
| 52 | _winapi.CloseHandle(rhandle) |
| 53 | raise |
| 54 | |
| 55 | # set attributes of self |
| 56 | self.pid = pid |
| 57 | self.returncode = None |
| 58 | self._handle = hp |
| 59 | self.sentinel = int(hp) |
| 60 | util.Finalize(self, _winapi.CloseHandle, (self.sentinel,)) |
| 61 | |
| 62 | # send information to child |
| 63 | popen.set_spawning_popen(self) |
| 64 | try: |
| 65 | reduction.dump(prep_data, to_child) |
| 66 | reduction.dump(process_obj, to_child) |
| 67 | finally: |
| 68 | popen.set_spawning_popen(None) |
| 69 | |
| 70 | def duplicate_for_child(self, handle): |
| 71 | assert self is popen.get_spawning_popen() |
| 72 | return reduction.duplicate(handle, self.sentinel) |
| 73 | |
| 74 | def wait(self, timeout=None): |
| 75 | if self.returncode is None: |
| 76 | if timeout is None: |
| 77 | msecs = _winapi.INFINITE |
| 78 | else: |
| 79 | msecs = max(0, int(timeout * 1000 + 0.5)) |
| 80 | |
| 81 | res = _winapi.WaitForSingleObject(int(self._handle), msecs) |
| 82 | if res == _winapi.WAIT_OBJECT_0: |
| 83 | code = _winapi.GetExitCodeProcess(self._handle) |
| 84 | if code == TERMINATE: |
| 85 | code = -signal.SIGTERM |
| 86 | self.returncode = code |
| 87 | |
| 88 | return self.returncode |
| 89 | |
| 90 | def poll(self): |
| 91 | return self.wait(timeout=0) |
| 92 | |
| 93 | def terminate(self): |
| 94 | if self.returncode is None: |
| 95 | try: |
| 96 | _winapi.TerminateProcess(int(self._handle), TERMINATE) |
| 97 | except OSError: |
| 98 | if self.wait(timeout=1.0) is None: |
| 99 | raise |
| 100 | |
| 101 | @staticmethod |
| 102 | def ensure_helpers_running(): |
| 103 | pass |