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