Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7acd50a

Browse files
authored
bpo-35491: Enhance multiprocessing.BaseProcess.__repr__() (GH-11138)
* Add the pid and parent pid to multiprocessing.BaseProcess.__repr__(). * Add negative sign (ex: "-SIGTERM") to exitcode (process killed by a signal) * Only call _popen.poll() once. Example: <ForkProcess(ForkPoolWorker-1, started daemon)> becomes: <ForkProcess name='ForkPoolWorker-1' pid=12449 parent=12448 started daemon> Example: <ForkProcess(ForkPoolWorker-1, stopped[SIGTERM] daemon)> becomes: <ForkProcess name='ForkPoolWorker-1' pid=12960 parent=12959 stopped exitcode=-SIGTERM daemon>
1 parent cb0f5e2 commit 7acd50a

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

Doc/library/multiprocessing.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,14 @@ The :mod:`multiprocessing` package mostly replicates the API of the
626626
>>> import multiprocessing, time, signal
627627
>>> p = multiprocessing.Process(target=time.sleep, args=(1000,))
628628
>>> print(p, p.is_alive())
629-
<Process(..., initial)> False
629+
<Process ... initial> False
630630
>>> p.start()
631631
>>> print(p, p.is_alive())
632-
<Process(..., started)> True
632+
<Process ... started> True
633633
>>> p.terminate()
634634
>>> time.sleep(0.1)
635635
>>> print(p, p.is_alive())
636-
<Process(..., stopped[SIGTERM])> False
636+
<Process ... stopped exitcode=-SIGTERM> False
637637
>>> p.exitcode == -signal.SIGTERM
638638
True
639639

Lib/multiprocessing/process.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def sentinel(self):
248248
raise ValueError("process not started") from None
249249

250250
def __repr__(self):
251+
exitcode = None
251252
if self is _current_process:
252253
status = 'started'
253254
elif self._closed:
@@ -257,19 +258,23 @@ def __repr__(self):
257258
elif self._popen is None:
258259
status = 'initial'
259260
else:
260-
if self._popen.poll() is not None:
261-
status = self.exitcode
262-
else:
263-
status = 'started'
264-
265-
if type(status) is int:
266-
if status == 0:
261+
exitcode = self._popen.poll()
262+
if exitcode is not None:
267263
status = 'stopped'
268264
else:
269-
status = 'stopped[%s]' % _exitcode_to_name.get(status, status)
265+
status = 'started'
270266

271-
return '<%s(%s, %s%s)>' % (type(self).__name__, self._name,
272-
status, self.daemon and ' daemon' or '')
267+
info = [type(self).__name__, 'name=%r' % self._name]
268+
if self._popen is not None:
269+
info.append('pid=%s' % self._popen.pid)
270+
info.append('parent=%s' % self._parent_pid)
271+
info.append(status)
272+
if exitcode is not None:
273+
exitcode = _exitcode_to_name.get(exitcode, exitcode)
274+
info.append('exitcode=%s' % exitcode)
275+
if self.daemon:
276+
info.append('daemon')
277+
return '<%s>' % ' '.join(info)
273278

274279
##
275280

@@ -373,7 +378,7 @@ def close(self):
373378

374379
for name, signum in list(signal.__dict__.items()):
375380
if name[:3]=='SIG' and '_' not in name:
376-
_exitcode_to_name[-signum] = name
381+
_exitcode_to_name[-signum] = f'-{name}'
377382

378383
# For debug and leak testing
379384
_dangling = WeakSet()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`multiprocessing`: Add ``Pool.__repr__()`` and enhance
2+
``BaseProcess.__repr__()`` (add pid and parent pid) to ease debugging. Pool
3+
state constant values are now strings instead of integers, for example ``RUN``
4+
value becomes ``'RUN'`` instead of ``0``.

0 commit comments

Comments
 (0)