ยปCore Development>Code coverage>Lib/multiprocessing/popen_forkserver.py

Python code coverage for Lib/multiprocessing/popen_forkserver.py

#countcontent
1n/aimport io
2n/aimport os
3n/a
4n/afrom .context import reduction, set_spawning_popen
5n/aif not reduction.HAVE_SEND_HANDLE:
6n/a raise ImportError('No support for sending fds between processes')
7n/afrom . import forkserver
8n/afrom . import popen_fork
9n/afrom . import spawn
10n/afrom . import util
11n/a
12n/a
13n/a__all__ = ['Popen']
14n/a
15n/a#
16n/a# Wrapper for an fd used while launching a process
17n/a#
18n/a
19n/aclass _DupFd(object):
20n/a def __init__(self, ind):
21n/a self.ind = ind
22n/a def detach(self):
23n/a return forkserver.get_inherited_fds()[self.ind]
24n/a
25n/a#
26n/a# Start child process using a server process
27n/a#
28n/a
29n/aclass Popen(popen_fork.Popen):
30n/a method = 'forkserver'
31n/a DupFd = _DupFd
32n/a
33n/a def __init__(self, process_obj):
34n/a self._fds = []
35n/a super().__init__(process_obj)
36n/a
37n/a def duplicate_for_child(self, fd):
38n/a self._fds.append(fd)
39n/a return len(self._fds) - 1
40n/a
41n/a def _launch(self, process_obj):
42n/a prep_data = spawn.get_preparation_data(process_obj._name)
43n/a buf = io.BytesIO()
44n/a set_spawning_popen(self)
45n/a try:
46n/a reduction.dump(prep_data, buf)
47n/a reduction.dump(process_obj, buf)
48n/a finally:
49n/a set_spawning_popen(None)
50n/a
51n/a self.sentinel, w = forkserver.connect_to_new_process(self._fds)
52n/a util.Finalize(self, os.close, (self.sentinel,))
53n/a with open(w, 'wb', closefd=True) as f:
54n/a f.write(buf.getbuffer())
55n/a self.pid = forkserver.read_unsigned(self.sentinel)
56n/a
57n/a def poll(self, flag=os.WNOHANG):
58n/a if self.returncode is None:
59n/a from multiprocessing.connection import wait
60n/a timeout = 0 if flag == os.WNOHANG else None
61n/a if not wait([self.sentinel], timeout):
62n/a return None
63n/a try:
64n/a self.returncode = forkserver.read_unsigned(self.sentinel)
65n/a except (OSError, EOFError):
66n/a # The process ended abnormally perhaps because of a signal
67n/a self.returncode = 255
68n/a return self.returncode