From 7acd50ad8b2a4fe132f7b26980ed3cd209b7ea12 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 14 Dec 2018 12:58:52 +0100 Subject: [PATCH] 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: becomes: Example: becomes: --- Doc/library/multiprocessing.rst | 6 ++--- Lib/multiprocessing/process.py | 27 +++++++++++-------- .../2018-12-14-12-12-15.bpo-35491.jHsNOU.rst | 4 +++ 3 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-14-12-12-15.bpo-35491.jHsNOU.rst diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 578b5483286..1d0920aa608 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -626,14 +626,14 @@ The :mod:`multiprocessing` package mostly replicates the API of the >>> import multiprocessing, time, signal >>> p = multiprocessing.Process(target=time.sleep, args=(1000,)) >>> print(p, p.is_alive()) - False + False >>> p.start() >>> print(p, p.is_alive()) - True + True >>> p.terminate() >>> time.sleep(0.1) >>> print(p, p.is_alive()) - False + False >>> p.exitcode == -signal.SIGTERM True diff --git a/Lib/multiprocessing/process.py b/Lib/multiprocessing/process.py index cd592d0bdf0..780f2d0c273 100644 --- a/Lib/multiprocessing/process.py +++ b/Lib/multiprocessing/process.py @@ -248,6 +248,7 @@ class BaseProcess(object): raise ValueError("process not started") from None def __repr__(self): + exitcode = None if self is _current_process: status = 'started' elif self._closed: @@ -257,19 +258,23 @@ class BaseProcess(object): elif self._popen is None: status = 'initial' else: - if self._popen.poll() is not None: - status = self.exitcode + exitcode = self._popen.poll() + if exitcode is not None: + status = 'stopped' else: status = 'started' - if type(status) is int: - if status == 0: - status = 'stopped' - else: - status = 'stopped[%s]' % _exitcode_to_name.get(status, status) - - return '<%s(%s, %s%s)>' % (type(self).__name__, self._name, - status, self.daemon and ' daemon' or '') + info = [type(self).__name__, 'name=%r' % self._name] + if self._popen is not None: + info.append('pid=%s' % self._popen.pid) + info.append('parent=%s' % self._parent_pid) + info.append(status) + if exitcode is not None: + exitcode = _exitcode_to_name.get(exitcode, exitcode) + info.append('exitcode=%s' % exitcode) + if self.daemon: + info.append('daemon') + return '<%s>' % ' '.join(info) ## @@ -373,7 +378,7 @@ _exitcode_to_name = {} for name, signum in list(signal.__dict__.items()): if name[:3]=='SIG' and '_' not in name: - _exitcode_to_name[-signum] = name + _exitcode_to_name[-signum] = f'-{name}' # For debug and leak testing _dangling = WeakSet() diff --git a/Misc/NEWS.d/next/Library/2018-12-14-12-12-15.bpo-35491.jHsNOU.rst b/Misc/NEWS.d/next/Library/2018-12-14-12-12-15.bpo-35491.jHsNOU.rst new file mode 100644 index 00000000000..7bb650ad734 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-14-12-12-15.bpo-35491.jHsNOU.rst @@ -0,0 +1,4 @@ +:mod:`multiprocessing`: Add ``Pool.__repr__()`` and enhance +``BaseProcess.__repr__()`` (add pid and parent pid) to ease debugging. Pool +state constant values are now strings instead of integers, for example ``RUN`` +value becomes ``'RUN'`` instead of ``0``.