blob: c96a4b7275605e3f4b4d4045dd017463dacf403a [file] [log] [blame]
Benjamin Petersone711caf2008-06-11 16:44:04 +00001#
2# Module providing the `Process` class which emulates `threading.Thread`
3#
4# multiprocessing/process.py
5#
6# Copyright (c) 2006-2008, R Oudkerk --- see COPYING.txt
7#
8
9__all__ = ['Process', 'current_process', 'active_children']
10
11#
12# Imports
13#
14
15import os
16import sys
17import signal
18import itertools
19
20#
21#
22#
23
24try:
25 ORIGINAL_DIR = os.path.abspath(os.getcwd())
26except OSError:
27 ORIGINAL_DIR = None
28
Benjamin Petersone711caf2008-06-11 16:44:04 +000029#
30# Public functions
31#
32
33def current_process():
34 '''
35 Return process object representing the current process
36 '''
37 return _current_process
38
39def active_children():
40 '''
41 Return list of process objects corresponding to live child processes
42 '''
43 _cleanup()
44 return list(_current_process._children)
45
46#
47#
48#
49
50def _cleanup():
51 # check for processes which have finished
52 for p in list(_current_process._children):
53 if p._popen.poll() is not None:
54 _current_process._children.discard(p)
55
56#
57# The `Process` class
58#
59
60class Process(object):
61 '''
62 Process objects represent activity that is run in a separate process
63
64 The class is analagous to `threading.Thread`
65 '''
66 _Popen = None
67
68 def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
69 assert group is None, 'group argument must be None for now'
70 count = next(_current_process._counter)
71 self._identity = _current_process._identity + (count,)
72 self._authkey = _current_process._authkey
73 self._daemonic = _current_process._daemonic
74 self._tempdir = _current_process._tempdir
75 self._parent_pid = os.getpid()
76 self._popen = None
77 self._target = target
78 self._args = tuple(args)
79 self._kwargs = dict(kwargs)
80 self._name = name or type(self).__name__ + '-' + \
81 ':'.join(str(i) for i in self._identity)
82
83 def run(self):
84 '''
85 Method to be run in sub-process; can be overridden in sub-class
86 '''
87 if self._target:
88 self._target(*self._args, **self._kwargs)
89
90 def start(self):
91 '''
92 Start child process
93 '''
94 assert self._popen is None, 'cannot start a process twice'
95 assert self._parent_pid == os.getpid(), \
96 'can only start a process object created by current process'
97 assert not _current_process._daemonic, \
98 'daemonic processes are not allowed to have children'
99 _cleanup()
100 if self._Popen is not None:
101 Popen = self._Popen
102 else:
103 from .forking import Popen
104 self._popen = Popen(self)
105 _current_process._children.add(self)
106
107 def terminate(self):
108 '''
109 Terminate process; sends SIGTERM signal or uses TerminateProcess()
110 '''
111 self._popen.terminate()
112
113 def join(self, timeout=None):
114 '''
115 Wait until child process terminates
116 '''
117 assert self._parent_pid == os.getpid(), 'can only join a child process'
118 assert self._popen is not None, 'can only join a started process'
119 res = self._popen.wait(timeout)
120 if res is not None:
121 _current_process._children.discard(self)
122
123 def is_alive(self):
124 '''
125 Return whether process is alive
126 '''
127 if self is _current_process:
128 return True
129 assert self._parent_pid == os.getpid(), 'can only test a child process'
130 if self._popen is None:
131 return False
132 self._popen.poll()
133 return self._popen.returncode is None
134
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000135 @property
136 def name(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000137 return self._name
138
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000139 @name.setter
140 def name(self, name):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000141 assert isinstance(name, str), 'name must be a string'
142 self._name = name
143
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000144 @property
145 def daemon(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000146 '''
147 Return whether process is a daemon
148 '''
149 return self._daemonic
150
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000151 @daemon.setter
152 def daemon(self, daemonic):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000153 '''
154 Set whether process is a daemon
155 '''
156 assert self._popen is None, 'process has already started'
157 self._daemonic = daemonic
158
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000159 @property
160 def authkey(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000161 return self._authkey
162
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000163 @authkey.setter
164 def authkey(self, authkey):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000165 '''
166 Set authorization key of process
167 '''
168 self._authkey = AuthenticationString(authkey)
169
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000170 @property
171 def exitcode(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000172 '''
173 Return exit code of process or `None` if it has yet to stop
174 '''
175 if self._popen is None:
176 return self._popen
177 return self._popen.poll()
178
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000179 @property
180 def ident(self):
Benjamin Petersone711caf2008-06-11 16:44:04 +0000181 '''
Florent Xiclunab519d232010-03-04 16:10:55 +0000182 Return identifier (PID) of process or `None` if it has yet to start
Benjamin Petersone711caf2008-06-11 16:44:04 +0000183 '''
184 if self is _current_process:
185 return os.getpid()
186 else:
187 return self._popen and self._popen.pid
188
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000189 pid = ident
Benjamin Petersone711caf2008-06-11 16:44:04 +0000190
191 def __repr__(self):
192 if self is _current_process:
193 status = 'started'
194 elif self._parent_pid != os.getpid():
195 status = 'unknown'
196 elif self._popen is None:
197 status = 'initial'
198 else:
199 if self._popen.poll() is not None:
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000200 status = self.exitcode
Benjamin Petersone711caf2008-06-11 16:44:04 +0000201 else:
202 status = 'started'
203
204 if type(status) is int:
205 if status == 0:
206 status = 'stopped'
207 else:
208 status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
209
210 return '<%s(%s, %s%s)>' % (type(self).__name__, self._name,
211 status, self._daemonic and ' daemon' or '')
212
213 ##
214
215 def _bootstrap(self):
216 from . import util
217 global _current_process
218
219 try:
220 self._children = set()
221 self._counter = itertools.count(1)
Amaury Forgeot d'Arc768008c2008-08-20 09:04:46 +0000222 if sys.stdin is not None:
223 try:
Alexandre Vassalottic57a84f2009-07-17 12:07:01 +0000224 sys.stdin.close()
225 sys.stdin = open(os.devnull)
Amaury Forgeot d'Arc768008c2008-08-20 09:04:46 +0000226 except (OSError, ValueError):
227 pass
Benjamin Petersone711caf2008-06-11 16:44:04 +0000228 _current_process = self
229 util._finalizer_registry.clear()
230 util._run_after_forkers()
231 util.info('child process calling self.run()')
232 try:
233 self.run()
234 exitcode = 0
235 finally:
236 util._exit_function()
237 except SystemExit as e:
238 if not e.args:
239 exitcode = 1
240 elif type(e.args[0]) is int:
241 exitcode = e.args[0]
242 else:
243 sys.stderr.write(e.args[0] + '\n')
244 sys.stderr.flush()
245 exitcode = 1
246 except:
247 exitcode = 1
248 import traceback
Benjamin Peterson58ea9fe2008-08-19 19:17:39 +0000249 sys.stderr.write('Process %s:\n' % self.name)
Benjamin Petersone711caf2008-06-11 16:44:04 +0000250 sys.stderr.flush()
251 traceback.print_exc()
252
253 util.info('process exiting with exitcode %d' % exitcode)
254 return exitcode
255
256#
257# We subclass bytes to avoid accidental transmission of auth keys over network
258#
259
260class AuthenticationString(bytes):
261 def __reduce__(self):
262 from .forking import Popen
263 if not Popen.thread_is_spawning():
264 raise TypeError(
265 'Pickling an AuthenticationString object is '
266 'disallowed for security reasons'
267 )
268 return AuthenticationString, (bytes(self),)
269
270#
271# Create object representing the main process
272#
273
274class _MainProcess(Process):
275
276 def __init__(self):
277 self._identity = ()
278 self._daemonic = False
279 self._name = 'MainProcess'
280 self._parent_pid = None
281 self._popen = None
282 self._counter = itertools.count(1)
283 self._children = set()
284 self._authkey = AuthenticationString(os.urandom(32))
285 self._tempdir = None
286
287_current_process = _MainProcess()
288del _MainProcess
289
290#
291# Give names to some return codes
292#
293
294_exitcode_to_name = {}
295
296for name, signum in list(signal.__dict__.items()):
297 if name[:3]=='SIG' and '_' not in name:
298 _exitcode_to_name[-signum] = name