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

Python code coverage for Lib/multiprocessing/popen_fork.py

#countcontent
1n/aimport os
2n/aimport sys
3n/aimport signal
4n/a
5n/afrom . import util
6n/a
7n/a__all__ = ['Popen']
8n/a
9n/a#
10n/a# Start child process using fork
11n/a#
12n/a
13n/aclass Popen(object):
14n/a method = 'fork'
15n/a
16n/a def __init__(self, process_obj):
17n/a sys.stdout.flush()
18n/a sys.stderr.flush()
19n/a self.returncode = None
20n/a self._launch(process_obj)
21n/a
22n/a def duplicate_for_child(self, fd):
23n/a return fd
24n/a
25n/a def poll(self, flag=os.WNOHANG):
26n/a if self.returncode is None:
27n/a while True:
28n/a try:
29n/a pid, sts = os.waitpid(self.pid, flag)
30n/a except OSError as e:
31n/a # Child process not yet created. See #1731717
32n/a # e.errno == errno.ECHILD == 10
33n/a return None
34n/a else:
35n/a break
36n/a if pid == self.pid:
37n/a if os.WIFSIGNALED(sts):
38n/a self.returncode = -os.WTERMSIG(sts)
39n/a else:
40n/a assert os.WIFEXITED(sts)
41n/a self.returncode = os.WEXITSTATUS(sts)
42n/a return self.returncode
43n/a
44n/a def wait(self, timeout=None):
45n/a if self.returncode is None:
46n/a if timeout is not None:
47n/a from multiprocessing.connection import wait
48n/a if not wait([self.sentinel], timeout):
49n/a return None
50n/a # This shouldn't block if wait() returned successfully.
51n/a return self.poll(os.WNOHANG if timeout == 0.0 else 0)
52n/a return self.returncode
53n/a
54n/a def terminate(self):
55n/a if self.returncode is None:
56n/a try:
57n/a os.kill(self.pid, signal.SIGTERM)
58n/a except ProcessLookupError:
59n/a pass
60n/a except OSError:
61n/a if self.wait(timeout=0.1) is None:
62n/a raise
63n/a
64n/a def _launch(self, process_obj):
65n/a code = 1
66n/a parent_r, child_w = os.pipe()
67n/a self.pid = os.fork()
68n/a if self.pid == 0:
69n/a try:
70n/a os.close(parent_r)
71n/a if 'random' in sys.modules:
72n/a import random
73n/a random.seed()
74n/a code = process_obj._bootstrap()
75n/a finally:
76n/a os._exit(code)
77n/a else:
78n/a os.close(child_w)
79n/a util.Finalize(self, os.close, (parent_r,))
80n/a self.sentinel = parent_r