From c53dfab2462af35bb82e12b639c05972e566a154 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 7 Jan 2020 23:03:30 +0100 Subject: [PATCH] Added pid_exists action --- platypush/plugins/system/__init__.py | 33 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/platypush/plugins/system/__init__.py b/platypush/plugins/system/__init__.py index 37882199..33e9cf92 100644 --- a/platypush/plugins/system/__init__.py +++ b/platypush/plugins/system/__init__.py @@ -588,6 +588,8 @@ class SystemPlugin(Plugin): args = {} try: + mem = p.memory_info() + times = p.cpu_times() args.update( pid=p.pid, name=p.name(), @@ -597,17 +599,17 @@ class SystemPlugin(Plugin): status=p.status(), username=p.username(), terminal=p.terminal(), - cpu_user_time=p.cpu_times().user, - cpu_system_time=p.cpu_times().system, - cpu_children_user_time=p.cpu_times().children_user, - cpu_children_system_time=p.cpu_times().children_system, - mem_rss=p.memory_info().rss, - mem_vms=p.memory_info().vms, - mem_shared=p.memory_info().shared, - mem_text=p.memory_info().text, - mem_data=p.memory_info().data, - mem_lib=p.memory_info().lib, - mem_dirty=p.memory_info().dirty, + cpu_user_time=times.user, + cpu_system_time=times.system, + cpu_children_user_time=times.children_user, + cpu_children_system_time=times.children_system, + mem_rss=mem.rss, + mem_vms=mem.vms, + mem_shared=mem.shared, + mem_text=mem.text, + mem_data=mem.data, + mem_lib=mem.lib, + mem_dirty=mem.dirty, mem_percent=p.memory_percent(), ) except psutil.Error: @@ -624,5 +626,14 @@ class SystemPlugin(Plugin): return ProcessResponseList(p_list) + @action + def pid_exists(self, pid: int) -> bool: + """ + :param pid: Process PID. + :return: ``True`` if the process exists, ``False`` otherwise. + """ + import psutil + return psutil.pid_exists(pid) + # vim:sw=4:ts=4:et: