-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathprefork.py
More file actions
314 lines (229 loc) · 8.36 KB
/
prefork.py
File metadata and controls
314 lines (229 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"""OpenMPI, once initialized, prohibits forking. This helper module
allows the forking of *one* helper child process before OpenMPI
initialization that can do the forking for the fork-challenged
parent process.
Since none of this is MPI-specific, it got parked in :mod:`pytools`.
.. autoexception:: ExecError
:show-inheritance:
.. autoclass:: Forker
.. autoclass:: DirectForker
.. autoclass:: IndirectForker
.. autofunction:: enable_prefork
.. autofunction:: call
.. autofunction:: call_async
.. autofunction:: call_capture_output
.. autofunction:: wait
.. autofunction:: waitall
"""
from __future__ import annotations
import socket
from abc import ABC, abstractmethod
from subprocess import Popen
from typing import TYPE_CHECKING, Any
from typing_extensions import override
if TYPE_CHECKING:
from collections.abc import Sequence
class ExecError(OSError):
pass
class Forker(ABC):
@abstractmethod
def call(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
pass
@abstractmethod
def call_async(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
pass
@abstractmethod
def call_capture_output(self,
cmdline: Sequence[str],
cwd: str | None = None,
error_on_nonzero: bool = True) -> tuple[int, bytes, bytes]:
pass
@abstractmethod
def wait(self, aid: int) -> int:
pass
@abstractmethod
def waitall(self) -> dict[int, int]:
pass
class DirectForker(Forker):
def __init__(self) -> None:
self.apids: dict[int, Popen[bytes]] = {}
self.count: int = 0
@override
def call(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
from subprocess import call as spcall
try:
return spcall(cmdline, cwd=cwd)
except OSError as e:
raise ExecError(
"error invoking '{}': {}".format(" ".join(cmdline), e)) from e
@override
def call_async(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
try:
self.count += 1
proc = Popen(cmdline, cwd=cwd)
self.apids[self.count] = proc
return self.count
except OSError as e:
raise ExecError(
"error invoking '{}': {}".format(" ".join(cmdline), e)) from e
@override
def call_capture_output(self,
cmdline: Sequence[str],
cwd: str | None = None,
error_on_nonzero: bool = True) -> tuple[int, bytes, bytes]:
from subprocess import PIPE, Popen
try:
popen = Popen(cmdline, cwd=cwd, stdin=PIPE, stdout=PIPE,
stderr=PIPE)
stdout_data, stderr_data = popen.communicate()
if error_on_nonzero and popen.returncode:
raise ExecError("status {} invoking '{}': {}".format(
popen.returncode,
" ".join(cmdline),
stderr_data.decode("utf-8", errors="replace")))
return popen.returncode, stdout_data, stderr_data
except OSError as e:
raise ExecError(
"error invoking '{}': {}".format(" ".join(cmdline), e)) from e
@override
def wait(self, aid: int) -> int:
proc = self.apids.pop(aid)
return proc.wait()
@override
def waitall(self) -> dict[int, int]:
rets = {}
for aid in self.apids:
rets[aid] = self.wait(aid)
return rets
def _send_packet(sock: socket.socket, data: object) -> None:
from pickle import dumps
from struct import pack
packet = dumps(data)
sock.sendall(pack("I", len(packet)))
sock.sendall(packet)
def _recv_packet(sock: socket.socket,
who: str = "Process",
partner: str = "other end") -> tuple[object, ...]:
from struct import calcsize, unpack
size_bytes_size = calcsize("I")
size_bytes = sock.recv(size_bytes_size)
if len(size_bytes) < size_bytes_size:
raise SystemExit
size, = unpack("I", size_bytes)
packet = b""
while len(packet) < size:
packet += sock.recv(size)
from pickle import loads
result = loads(packet)
assert isinstance(result, tuple)
return result
def _fork_server(sock: socket.socket) -> None:
# Ignore keyboard interrupts, we'll get notified by the parent.
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Construct a local DirectForker to do the dirty work
df = DirectForker()
funcs = {
"call": df.call,
"call_async": df.call_async,
"call_capture_output": df.call_capture_output,
"wait": df.wait,
"waitall": df.waitall
}
try:
while True:
func_name, args, kwargs = _recv_packet(
sock, who="Prefork server", partner="parent"
)
assert isinstance(func_name, str)
if func_name == "quit":
df.waitall()
_send_packet(sock, ("ok", None))
break
try:
result = funcs[func_name](*args, **kwargs)
# FIXME: Is catching all exceptions the right course of action?
except Exception as e: # pylint:disable=broad-except
_send_packet(sock, ("exception", e))
else:
_send_packet(sock, ("ok", result))
finally:
sock.close()
import os
os._exit(0)
class IndirectForker(Forker):
def __init__(self, server_pid: int, sock: socket.socket) -> None:
self.server_pid = server_pid
self.socket = sock
import atexit
atexit.register(self._quit)
def _remote_invoke(self, name: str, *args: Any, **kwargs: Any) -> object:
_send_packet(self.socket, (name, args, kwargs))
status, result = _recv_packet(
self.socket, who="Prefork client", partner="prefork server"
)
if status == "exception":
assert isinstance(result, Exception)
raise result
assert status == "ok"
return result
def _quit(self) -> None:
self._remote_invoke("quit")
from os import waitpid
waitpid(self.server_pid, 0)
@override
def call(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
result = self._remote_invoke("call", cmdline, cwd)
assert isinstance(result, int)
return result
@override
def call_async(self, cmdline: Sequence[str], cwd: str | None = None) -> int:
result = self._remote_invoke("call_async", cmdline, cwd)
assert isinstance(result, int)
return result
@override
def call_capture_output(self,
cmdline: Sequence[str],
cwd: str | None = None,
error_on_nonzero: bool = True,
) -> tuple[int, bytes, bytes]:
return self._remote_invoke("call_capture_output", cmdline, cwd,
error_on_nonzero)
@override
def wait(self, aid: int) -> int:
result = self._remote_invoke("wait", aid)
assert isinstance(result, int)
return result
@override
def waitall(self) -> dict[int, int]:
result = self._remote_invoke("waitall")
assert isinstance(result, dict)
return result
forker: Forker = DirectForker()
def enable_prefork() -> None:
global forker
if isinstance(forker, IndirectForker):
return
s_parent, s_child = socket.socketpair()
from os import fork
fork_res = fork()
# Child
if fork_res == 0:
s_parent.close()
_fork_server(s_child)
# Parent
else:
s_child.close()
forker = IndirectForker(fork_res, s_parent)
def call(cmdline: Sequence[str], cwd: str | None = None) -> int:
return forker.call(cmdline, cwd)
def call_async(cmdline: Sequence[str], cwd: str | None = None) -> int:
return forker.call_async(cmdline, cwd)
def call_capture_output(cmdline: Sequence[str],
cwd: str | None = None,
error_on_nonzero: bool = True) -> tuple[int, bytes, bytes]:
return forker.call_capture_output(cmdline, cwd, error_on_nonzero)
def wait(aid: int) -> int:
return forker.wait(aid)
def waitall() -> dict[int, int]:
return forker.waitall()