Debian Codegraph @

Lib/multiprocessing/process.py

raw
1 #
2 # Module providing the `Process` class which emulates `threading.Thread`
3 #
4 # multiprocessing/process.py
5 #
6 # Copyright (c) 2006-2008, R Oudkerk
7 # Licensed to PSF under a Contributor Agreement.
8 #
9
10 __all__ = ['BaseProcess', 'current_process', 'active_children',
11 'parent_process']
12
13 #
14 # Imports
15 #
16
17 import os
18 import sys
19 import signal
20 import itertools
21 import threading
22 from _weakrefset import WeakSet
23
24 #
25 #
26 #
27
28 try:
29 ORIGINAL_DIR = os.path.abspath(os.getcwd())
30 except OSError:
31 ORIGINAL_DIR = None
32
33 #
34 # Public functions
35 #
36
37 def current_process():
38 '''
39 Return process object representing the current process
40 '''
41 return _current_process
42
43 def active_children():
44 '''
45 Return list of process objects corresponding to live child processes
46 '''
47 _cleanup()
48 return list(_children)
49
50
51 def parent_process():
52 '''
53 Return process object representing the parent process
54 '''
55 return _parent_process
56
57 #
58 #
59 #
60
61 def _cleanup():
62 # check for processes which have finished
63 for p in list(_children):
64 if (child_popen := p._popen) and child_popen.poll() is not None:
65 _children.discard(p)
66
67 #
68 # The `Process` class
69 #
70
71 class BaseProcess(object):
72 '''
73 Process objects represent activity that is run in a separate process
74
75 The class is analogous to `threading.Thread`
76 '''
77 def _Popen(self):
78 raise NotImplementedError
79
80 def __init__(self, group=None, target=None, name=None, args=(), kwargs=None,
81 *, daemon=None):
82 assert group is None, 'group argument must be None for now'
83 count = next(_process_counter)
84 self._identity = _current_process._identity + (count,)
85 self._config = _current_process._config.copy()
86 self._parent_pid = os.getpid()
87 self._parent_name = _current_process.name
88 self._popen = None
89 self._closed = False
90 self._target = target
91 self._args = tuple(args)
92 self._kwargs = dict(kwargs) if kwargs else {}
93 self._name = name or type(self).__name__ + '-' + \
94 ':'.join(str(i) for i in self._identity)
95 if daemon is not None:
96 self.daemon = daemon
97 _dangling.add(self)
98
99 def _check_closed(self):
100 if self._closed:
101 raise ValueError("process object is closed")
102
103 def run(self):
104 '''
105 Method to be run in sub-process; can be overridden in sub-class
106 '''
107 if self._target:
108 self._target(*self._args, **self._kwargs)
109
110 def start(self):
111 '''
112 Start child process
113 '''
114 self._check_closed()
115 assert self._popen is None, 'cannot start a process twice'
116 assert self._parent_pid == os.getpid(), \
117 'can only start a process object created by current process'
118 assert not _current_process._config.get('daemon'), \
119 'daemonic processes are not allowed to have children'
120 _cleanup()
121 self._popen = self._Popen(self)
122 self._sentinel = self._popen.sentinel
123 # Avoid a refcycle if the target function holds an indirect
124 # reference to the process object (see bpo-30775)
125 del self._target, self._args, self._kwargs
126 _children.add(self)
127
128 def interrupt(self):
129 '''
130 Terminate process; sends SIGINT signal
131 '''
132 self._check_closed()
133 self._popen.interrupt()
134
135 def terminate(self):
136 '''
137 Terminate process; sends SIGTERM signal or uses TerminateProcess()
138 '''
139 self._check_closed()
140 self._popen.terminate()
141
142 def kill(self):
143 '''
144 Terminate process; sends SIGKILL signal or uses TerminateProcess()
145 '''
146 self._check_closed()
147 self._popen.kill()
148
149 def join(self, timeout=None):
150 '''
151 Wait until child process terminates
152 '''
153 self._check_closed()
154 assert self._parent_pid == os.getpid(), 'can only join a child process'
155 assert self._popen is not None, 'can only join a started process'
156 res = self._popen.wait(timeout)
157 if res is not None:
158 _children.discard(self)
159
160 def is_alive(self):
161 '''
162 Return whether process is alive
163 '''
164 self._check_closed()
165 if self is _current_process:
166 return True
167 assert self._parent_pid == os.getpid(), 'can only test a child process'
168
169 if self._popen is None:
170 return False
171
172 returncode = self._popen.poll()
173 if returncode is None:
174 return True
175 else:
176 _children.discard(self)
177 return False
178
179 def close(self):
180 '''
181 Close the Process object.
182
183 This method releases resources held by the Process object. It is
184 an error to call this method if the child process is still running.
185 '''
186 if self._popen is not None:
187 if self._popen.poll() is None:
188 raise ValueError("Cannot close a process while it is still running. "
189 "You should first call join() or terminate().")
190 self._popen.close()
191 self._popen = None
192 del self._sentinel
193 _children.discard(self)
194 self._closed = True
195
196 @property
197 def name(self):
198 return self._name
199
200 @name.setter
201 def name(self, name):
202 assert isinstance(name, str), 'name must be a string'
203 self._name = name
204
205 @property
206 def daemon(self):
207 '''
208 Return whether process is a daemon
209 '''
210 return self._config.get('daemon', False)
211
212 @daemon.setter
213 def daemon(self, daemonic):
214 '''
215 Set whether process is a daemon
216 '''
217 assert self._popen is None, 'process has already started'
218 self._config['daemon'] = daemonic
219
220 @property
221 def authkey(self):
222 return self._config['authkey']
223
224 @authkey.setter
225 def authkey(self, authkey):
226 '''
227 Set authorization key of process
228 '''
229 self._config['authkey'] = AuthenticationString(authkey)
230
231 @property
232 def exitcode(self):
233 '''
234 Return exit code of process or `None` if it has yet to stop
235 '''
236 self._check_closed()
237 if self._popen is None:
238 return self._popen
239 return self._popen.poll()
240
241 @property
242 def ident(self):
243 '''
244 Return identifier (PID) of process or `None` if it has yet to start
245 '''
246 self._check_closed()
247 if self is _current_process:
248 return os.getpid()
249 else:
250 return self._popen and self._popen.pid
251
252 pid = ident
253
254 @property
255 def sentinel(self):
256 '''
257 Return a file descriptor (Unix) or handle (Windows) suitable for
258 waiting for process termination.
259 '''
260 self._check_closed()
261 try:
262 return self._sentinel
263 except AttributeError:
264 raise ValueError("process not started") from None
265
266 def __repr__(self):
267 exitcode = None
268 if self is _current_process:
269 status = 'started'
270 elif self._closed:
271 status = 'closed'
272 elif self._parent_pid != os.getpid():
273 status = 'unknown'
274 elif self._popen is None:
275 status = 'initial'
276 else:
277 exitcode = self._popen.poll()
278 if exitcode is not None:
279 status = 'stopped'
280 else:
281 status = 'started'
282
283 info = [type(self).__name__, 'name=%r' % self._name]
284 if self._popen is not None:
285 info.append('pid=%s' % self._popen.pid)
286 info.append('parent=%s' % self._parent_pid)
287 info.append(status)
288 if exitcode is not None:
289 exitcode = _exitcode_to_name.get(exitcode, exitcode)
290 info.append('exitcode=%s' % exitcode)
291 if self.daemon:
292 info.append('daemon')
293 return '<%s>' % ' '.join(info)
294
295 ##
296
297 def _bootstrap(self, parent_sentinel=None):
298 from . import util, context
299 global _current_process, _parent_process, _process_counter, _children
300
301 try:
302 if self._start_method is not None:
303 context._force_start_method(self._start_method)
304 _process_counter = itertools.count(1)
305 _children = set()
306 util._close_stdin()
307 old_process = _current_process
308 _current_process = self
309 _parent_process = _ParentProcess(
310 self._parent_name, self._parent_pid, parent_sentinel)
311 if threading._HAVE_THREAD_NATIVE_ID:
312 threading.main_thread()._set_native_id()
313 try:
314 self._after_fork()
315 finally:
316 # delay finalization of the old process object until after
317 # _run_after_forkers() is executed
318 del old_process
319 util.info('child process calling self.run()')
320 self.run()
321 exitcode = 0
322 except SystemExit as e:
323 if e.code is None:
324 exitcode = 0
325 elif isinstance(e.code, int):
326 exitcode = e.code
327 else:
328 sys.stderr.write(str(e.code) + '\n')
329 exitcode = 1
330 except:
331 exitcode = 1
332 import traceback
333 sys.stderr.write('Process %s:\n' % self.name)
334 traceback.print_exc()
335 finally:
336 threading._shutdown()
337 util.info('process exiting with exitcode %d' % exitcode)
338 util._flush_std_streams()
339
340 return exitcode
341
342 @staticmethod
343 def _after_fork():
344 from . import util
345 util._finalizer_registry.clear()
346 util._run_after_forkers()
347
348
349 #
350 # We subclass bytes to avoid accidental transmission of auth keys over network
351 #
352
353 class AuthenticationString(bytes):
354 def __reduce__(self):
355 from .context import get_spawning_popen
356 if get_spawning_popen() is None:
357 raise TypeError(
358 'Pickling an AuthenticationString object is '
359 'disallowed for security reasons'
360 )
361 return AuthenticationString, (bytes(self),)
362
363
364 #
365 # Create object representing the parent process
366 #
367
368 class _ParentProcess(BaseProcess):
369
370 def __init__(self, name, pid, sentinel):
371 self._identity = ()
372 self._name = name
373 self._pid = pid
374 self._parent_pid = None
375 self._popen = None
376 self._closed = False
377 self._sentinel = sentinel
378 self._config = {}
379
380 def is_alive(self):
381 from multiprocessing.connection import wait
382 return not wait([self._sentinel], timeout=0)
383
384 @property
385 def ident(self):
386 return self._pid
387
388 def join(self, timeout=None):
389 '''
390 Wait until parent process terminates
391 '''
392 from multiprocessing.connection import wait
393 wait([self._sentinel], timeout=timeout)
394
395 pid = ident
396
397 #
398 # Create object representing the main process
399 #
400
401 class _MainProcess(BaseProcess):
402
403 def __init__(self):
404 self._identity = ()
405 self._name = 'MainProcess'
406 self._parent_pid = None
407 self._popen = None
408 self._closed = False
409 self._config = {'authkey': AuthenticationString(os.urandom(32)),
410 'semprefix': '/mp'}
411 # Note that some versions of FreeBSD only allow named
412 # semaphores to have names of up to 14 characters. Therefore
413 # we choose a short prefix.
414 #
415 # On MacOSX in a sandbox it may be necessary to use a
416 # different prefix -- see #19478.
417 #
418 # Everything in self._config will be inherited by descendant
419 # processes.
420
421 def close(self):
422 pass
423
424
425 _parent_process = None
426 _current_process = _MainProcess()
427 _process_counter = itertools.count(1)
428 _children = set()
429 del _MainProcess
430
431 #
432 # Give names to some return codes
433 #
434
435 _exitcode_to_name = {}
436
437 for name, signum in list(signal.__dict__.items()):
438 if name[:3]=='SIG' and '_' not in name:
439 _exitcode_to_name[-signum] = f'-{name}'
440 del name, signum
441
442 # For debug and leak testing
443 _dangling = WeakSet()