1 | n/a | # |
---|
2 | n/a | # Module providing the `Process` class which emulates `threading.Thread` |
---|
3 | n/a | # |
---|
4 | n/a | # multiprocessing/process.py |
---|
5 | n/a | # |
---|
6 | n/a | # Copyright (c) 2006-2008, R Oudkerk |
---|
7 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
8 | n/a | # |
---|
9 | n/a | |
---|
10 | n/a | __all__ = ['BaseProcess', 'current_process', 'active_children'] |
---|
11 | n/a | |
---|
12 | n/a | # |
---|
13 | n/a | # Imports |
---|
14 | n/a | # |
---|
15 | n/a | |
---|
16 | n/a | import os |
---|
17 | n/a | import sys |
---|
18 | n/a | import signal |
---|
19 | n/a | import itertools |
---|
20 | n/a | from _weakrefset import WeakSet |
---|
21 | n/a | |
---|
22 | n/a | # |
---|
23 | n/a | # |
---|
24 | n/a | # |
---|
25 | n/a | |
---|
26 | n/a | try: |
---|
27 | n/a | ORIGINAL_DIR = os.path.abspath(os.getcwd()) |
---|
28 | n/a | except OSError: |
---|
29 | n/a | ORIGINAL_DIR = None |
---|
30 | n/a | |
---|
31 | n/a | # |
---|
32 | n/a | # Public functions |
---|
33 | n/a | # |
---|
34 | n/a | |
---|
35 | n/a | def current_process(): |
---|
36 | n/a | ''' |
---|
37 | n/a | Return process object representing the current process |
---|
38 | n/a | ''' |
---|
39 | n/a | return _current_process |
---|
40 | n/a | |
---|
41 | n/a | def active_children(): |
---|
42 | n/a | ''' |
---|
43 | n/a | Return list of process objects corresponding to live child processes |
---|
44 | n/a | ''' |
---|
45 | n/a | _cleanup() |
---|
46 | n/a | return list(_children) |
---|
47 | n/a | |
---|
48 | n/a | # |
---|
49 | n/a | # |
---|
50 | n/a | # |
---|
51 | n/a | |
---|
52 | n/a | def _cleanup(): |
---|
53 | n/a | # check for processes which have finished |
---|
54 | n/a | for p in list(_children): |
---|
55 | n/a | if p._popen.poll() is not None: |
---|
56 | n/a | _children.discard(p) |
---|
57 | n/a | |
---|
58 | n/a | # |
---|
59 | n/a | # The `Process` class |
---|
60 | n/a | # |
---|
61 | n/a | |
---|
62 | n/a | class BaseProcess(object): |
---|
63 | n/a | ''' |
---|
64 | n/a | Process objects represent activity that is run in a separate process |
---|
65 | n/a | |
---|
66 | n/a | The class is analogous to `threading.Thread` |
---|
67 | n/a | ''' |
---|
68 | n/a | def _Popen(self): |
---|
69 | n/a | raise NotImplementedError |
---|
70 | n/a | |
---|
71 | n/a | def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, |
---|
72 | n/a | *, daemon=None): |
---|
73 | n/a | assert group is None, 'group argument must be None for now' |
---|
74 | n/a | count = next(_process_counter) |
---|
75 | n/a | self._identity = _current_process._identity + (count,) |
---|
76 | n/a | self._config = _current_process._config.copy() |
---|
77 | n/a | self._parent_pid = os.getpid() |
---|
78 | n/a | self._popen = None |
---|
79 | n/a | self._target = target |
---|
80 | n/a | self._args = tuple(args) |
---|
81 | n/a | self._kwargs = dict(kwargs) |
---|
82 | n/a | self._name = name or type(self).__name__ + '-' + \ |
---|
83 | n/a | ':'.join(str(i) for i in self._identity) |
---|
84 | n/a | if daemon is not None: |
---|
85 | n/a | self.daemon = daemon |
---|
86 | n/a | _dangling.add(self) |
---|
87 | n/a | |
---|
88 | n/a | def run(self): |
---|
89 | n/a | ''' |
---|
90 | n/a | Method to be run in sub-process; can be overridden in sub-class |
---|
91 | n/a | ''' |
---|
92 | n/a | if self._target: |
---|
93 | n/a | self._target(*self._args, **self._kwargs) |
---|
94 | n/a | |
---|
95 | n/a | def start(self): |
---|
96 | n/a | ''' |
---|
97 | n/a | Start child process |
---|
98 | n/a | ''' |
---|
99 | n/a | assert self._popen is None, 'cannot start a process twice' |
---|
100 | n/a | assert self._parent_pid == os.getpid(), \ |
---|
101 | n/a | 'can only start a process object created by current process' |
---|
102 | n/a | assert not _current_process._config.get('daemon'), \ |
---|
103 | n/a | 'daemonic processes are not allowed to have children' |
---|
104 | n/a | _cleanup() |
---|
105 | n/a | self._popen = self._Popen(self) |
---|
106 | n/a | self._sentinel = self._popen.sentinel |
---|
107 | n/a | _children.add(self) |
---|
108 | n/a | |
---|
109 | n/a | def terminate(self): |
---|
110 | n/a | ''' |
---|
111 | n/a | Terminate process; sends SIGTERM signal or uses TerminateProcess() |
---|
112 | n/a | ''' |
---|
113 | n/a | self._popen.terminate() |
---|
114 | n/a | |
---|
115 | n/a | def join(self, timeout=None): |
---|
116 | n/a | ''' |
---|
117 | n/a | Wait until child process terminates |
---|
118 | n/a | ''' |
---|
119 | n/a | assert self._parent_pid == os.getpid(), 'can only join a child process' |
---|
120 | n/a | assert self._popen is not None, 'can only join a started process' |
---|
121 | n/a | res = self._popen.wait(timeout) |
---|
122 | n/a | if res is not None: |
---|
123 | n/a | _children.discard(self) |
---|
124 | n/a | |
---|
125 | n/a | def is_alive(self): |
---|
126 | n/a | ''' |
---|
127 | n/a | Return whether process is alive |
---|
128 | n/a | ''' |
---|
129 | n/a | if self is _current_process: |
---|
130 | n/a | return True |
---|
131 | n/a | assert self._parent_pid == os.getpid(), 'can only test a child process' |
---|
132 | n/a | if self._popen is None: |
---|
133 | n/a | return False |
---|
134 | n/a | self._popen.poll() |
---|
135 | n/a | return self._popen.returncode is None |
---|
136 | n/a | |
---|
137 | n/a | @property |
---|
138 | n/a | def name(self): |
---|
139 | n/a | return self._name |
---|
140 | n/a | |
---|
141 | n/a | @name.setter |
---|
142 | n/a | def name(self, name): |
---|
143 | n/a | assert isinstance(name, str), 'name must be a string' |
---|
144 | n/a | self._name = name |
---|
145 | n/a | |
---|
146 | n/a | @property |
---|
147 | n/a | def daemon(self): |
---|
148 | n/a | ''' |
---|
149 | n/a | Return whether process is a daemon |
---|
150 | n/a | ''' |
---|
151 | n/a | return self._config.get('daemon', False) |
---|
152 | n/a | |
---|
153 | n/a | @daemon.setter |
---|
154 | n/a | def daemon(self, daemonic): |
---|
155 | n/a | ''' |
---|
156 | n/a | Set whether process is a daemon |
---|
157 | n/a | ''' |
---|
158 | n/a | assert self._popen is None, 'process has already started' |
---|
159 | n/a | self._config['daemon'] = daemonic |
---|
160 | n/a | |
---|
161 | n/a | @property |
---|
162 | n/a | def authkey(self): |
---|
163 | n/a | return self._config['authkey'] |
---|
164 | n/a | |
---|
165 | n/a | @authkey.setter |
---|
166 | n/a | def authkey(self, authkey): |
---|
167 | n/a | ''' |
---|
168 | n/a | Set authorization key of process |
---|
169 | n/a | ''' |
---|
170 | n/a | self._config['authkey'] = AuthenticationString(authkey) |
---|
171 | n/a | |
---|
172 | n/a | @property |
---|
173 | n/a | def exitcode(self): |
---|
174 | n/a | ''' |
---|
175 | n/a | Return exit code of process or `None` if it has yet to stop |
---|
176 | n/a | ''' |
---|
177 | n/a | if self._popen is None: |
---|
178 | n/a | return self._popen |
---|
179 | n/a | return self._popen.poll() |
---|
180 | n/a | |
---|
181 | n/a | @property |
---|
182 | n/a | def ident(self): |
---|
183 | n/a | ''' |
---|
184 | n/a | Return identifier (PID) of process or `None` if it has yet to start |
---|
185 | n/a | ''' |
---|
186 | n/a | if self is _current_process: |
---|
187 | n/a | return os.getpid() |
---|
188 | n/a | else: |
---|
189 | n/a | return self._popen and self._popen.pid |
---|
190 | n/a | |
---|
191 | n/a | pid = ident |
---|
192 | n/a | |
---|
193 | n/a | @property |
---|
194 | n/a | def sentinel(self): |
---|
195 | n/a | ''' |
---|
196 | n/a | Return a file descriptor (Unix) or handle (Windows) suitable for |
---|
197 | n/a | waiting for process termination. |
---|
198 | n/a | ''' |
---|
199 | n/a | try: |
---|
200 | n/a | return self._sentinel |
---|
201 | n/a | except AttributeError: |
---|
202 | n/a | raise ValueError("process not started") |
---|
203 | n/a | |
---|
204 | n/a | def __repr__(self): |
---|
205 | n/a | if self is _current_process: |
---|
206 | n/a | status = 'started' |
---|
207 | n/a | elif self._parent_pid != os.getpid(): |
---|
208 | n/a | status = 'unknown' |
---|
209 | n/a | elif self._popen is None: |
---|
210 | n/a | status = 'initial' |
---|
211 | n/a | else: |
---|
212 | n/a | if self._popen.poll() is not None: |
---|
213 | n/a | status = self.exitcode |
---|
214 | n/a | else: |
---|
215 | n/a | status = 'started' |
---|
216 | n/a | |
---|
217 | n/a | if type(status) is int: |
---|
218 | n/a | if status == 0: |
---|
219 | n/a | status = 'stopped' |
---|
220 | n/a | else: |
---|
221 | n/a | status = 'stopped[%s]' % _exitcode_to_name.get(status, status) |
---|
222 | n/a | |
---|
223 | n/a | return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, |
---|
224 | n/a | status, self.daemon and ' daemon' or '') |
---|
225 | n/a | |
---|
226 | n/a | ## |
---|
227 | n/a | |
---|
228 | n/a | def _bootstrap(self): |
---|
229 | n/a | from . import util, context |
---|
230 | n/a | global _current_process, _process_counter, _children |
---|
231 | n/a | |
---|
232 | n/a | try: |
---|
233 | n/a | if self._start_method is not None: |
---|
234 | n/a | context._force_start_method(self._start_method) |
---|
235 | n/a | _process_counter = itertools.count(1) |
---|
236 | n/a | _children = set() |
---|
237 | n/a | util._close_stdin() |
---|
238 | n/a | old_process = _current_process |
---|
239 | n/a | _current_process = self |
---|
240 | n/a | try: |
---|
241 | n/a | util._finalizer_registry.clear() |
---|
242 | n/a | util._run_after_forkers() |
---|
243 | n/a | finally: |
---|
244 | n/a | # delay finalization of the old process object until after |
---|
245 | n/a | # _run_after_forkers() is executed |
---|
246 | n/a | del old_process |
---|
247 | n/a | util.info('child process calling self.run()') |
---|
248 | n/a | try: |
---|
249 | n/a | self.run() |
---|
250 | n/a | exitcode = 0 |
---|
251 | n/a | finally: |
---|
252 | n/a | util._exit_function() |
---|
253 | n/a | except SystemExit as e: |
---|
254 | n/a | if not e.args: |
---|
255 | n/a | exitcode = 1 |
---|
256 | n/a | elif isinstance(e.args[0], int): |
---|
257 | n/a | exitcode = e.args[0] |
---|
258 | n/a | else: |
---|
259 | n/a | sys.stderr.write(str(e.args[0]) + '\n') |
---|
260 | n/a | exitcode = 1 |
---|
261 | n/a | except: |
---|
262 | n/a | exitcode = 1 |
---|
263 | n/a | import traceback |
---|
264 | n/a | sys.stderr.write('Process %s:\n' % self.name) |
---|
265 | n/a | traceback.print_exc() |
---|
266 | n/a | finally: |
---|
267 | n/a | util.info('process exiting with exitcode %d' % exitcode) |
---|
268 | n/a | sys.stdout.flush() |
---|
269 | n/a | sys.stderr.flush() |
---|
270 | n/a | |
---|
271 | n/a | return exitcode |
---|
272 | n/a | |
---|
273 | n/a | # |
---|
274 | n/a | # We subclass bytes to avoid accidental transmission of auth keys over network |
---|
275 | n/a | # |
---|
276 | n/a | |
---|
277 | n/a | class AuthenticationString(bytes): |
---|
278 | n/a | def __reduce__(self): |
---|
279 | n/a | from .context import get_spawning_popen |
---|
280 | n/a | if get_spawning_popen() is None: |
---|
281 | n/a | raise TypeError( |
---|
282 | n/a | 'Pickling an AuthenticationString object is ' |
---|
283 | n/a | 'disallowed for security reasons' |
---|
284 | n/a | ) |
---|
285 | n/a | return AuthenticationString, (bytes(self),) |
---|
286 | n/a | |
---|
287 | n/a | # |
---|
288 | n/a | # Create object representing the main process |
---|
289 | n/a | # |
---|
290 | n/a | |
---|
291 | n/a | class _MainProcess(BaseProcess): |
---|
292 | n/a | |
---|
293 | n/a | def __init__(self): |
---|
294 | n/a | self._identity = () |
---|
295 | n/a | self._name = 'MainProcess' |
---|
296 | n/a | self._parent_pid = None |
---|
297 | n/a | self._popen = None |
---|
298 | n/a | self._config = {'authkey': AuthenticationString(os.urandom(32)), |
---|
299 | n/a | 'semprefix': '/mp'} |
---|
300 | n/a | # Note that some versions of FreeBSD only allow named |
---|
301 | n/a | # semaphores to have names of up to 14 characters. Therefore |
---|
302 | n/a | # we choose a short prefix. |
---|
303 | n/a | # |
---|
304 | n/a | # On MacOSX in a sandbox it may be necessary to use a |
---|
305 | n/a | # different prefix -- see #19478. |
---|
306 | n/a | # |
---|
307 | n/a | # Everything in self._config will be inherited by descendant |
---|
308 | n/a | # processes. |
---|
309 | n/a | |
---|
310 | n/a | |
---|
311 | n/a | _current_process = _MainProcess() |
---|
312 | n/a | _process_counter = itertools.count(1) |
---|
313 | n/a | _children = set() |
---|
314 | n/a | del _MainProcess |
---|
315 | n/a | |
---|
316 | n/a | # |
---|
317 | n/a | # Give names to some return codes |
---|
318 | n/a | # |
---|
319 | n/a | |
---|
320 | n/a | _exitcode_to_name = {} |
---|
321 | n/a | |
---|
322 | n/a | for name, signum in list(signal.__dict__.items()): |
---|
323 | n/a | if name[:3]=='SIG' and '_' not in name: |
---|
324 | n/a | _exitcode_to_name[-signum] = name |
---|
325 | n/a | |
---|
326 | n/a | # For debug and leak testing |
---|
327 | n/a | _dangling = WeakSet() |
---|