From 67b5fe1a1664a9eef2cf68938df7c56245ef217f Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 6 Jan 2024 11:00:01 +0100 Subject: [PATCH] Crash when a process is pinned in the develop branch of Glances #2639 --- glances/globals.py | 10 ++++++++++ glances/processes.py | 9 ++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/glances/globals.py b/glances/globals.py index e4e42ec8..a88af241 100644 --- a/glances/globals.py +++ b/glances/globals.py @@ -426,3 +426,13 @@ def weak_lru_cache(maxsize=128, typed=False): return inner return wrapper + + +def namedtuple_to_dict(data): + """Convert a namedtuple to a dict, using the _asdict() method embeded in PsUtil stats.""" + return {k: (v._asdict() if hasattr(v, '_asdict') else v) for k, v in data.items()} + + +def list_of_namedtuple_to_list_of_dict(data): + """Convert a list of namedtuples to a dict, using the _asdict() method embeded in PsUtil stats.""" + return [namedtuple_to_dict(d) for d in data] diff --git a/glances/processes.py b/glances/processes.py index 74606786..7666420f 100644 --- a/glances/processes.py +++ b/glances/processes.py @@ -10,6 +10,7 @@ import os from glances.globals import BSD, LINUX, MACOS, WINDOWS, iterkeys +from glances.globals import namedtuple_to_dict, list_of_namedtuple_to_list_of_dict from glances.timer import Timer, getTimeSinceLastUpdate from glances.filter import GlancesFilter from glances.programs import processes_to_programs @@ -336,7 +337,7 @@ class GlancesProcesses(object): ret[stat_prefix + '_mean'] = ret[stat_prefix + '_mean_sum'] / ret[stat_prefix + '_mean_counter'] ret['extended_stats'] = True - return ret + return namedtuple_to_dict(ret) def is_selected_extended_process(self, position): """Return True if the process is the selected one for extended stats.""" @@ -422,7 +423,7 @@ class GlancesProcesses(object): # Grab extended stats only for the selected process (see issue #2225) if self.extended_process is not None and proc['pid'] == self.extended_process['pid']: proc.update(self.get_extended_stats(self.extended_process)) - self.extended_process = proc + self.extended_process = namedtuple_to_dict(proc) # Meta data ########### @@ -485,9 +486,7 @@ class GlancesProcesses(object): processlist = list(filter(lambda p: not self._filter.is_filtered(p), processlist)) # Save the new processlist and transform all namedtuples to dict - self.processlist = [ - {k: (v._asdict() if hasattr(v, '_asdict') else v) for k, v in p.items()} for p in processlist - ] + self.processlist = list_of_namedtuple_to_list_of_dict(processlist) # Compute the maximum value for keys in self._max_values_list: CPU, MEM # Useful to highlight the processes with maximum values