打印进程树,结合psutil
import psutil
from functools import wraps
class SlotBase(object):
def __init__(self, *args, **kwargs):
setted = set()
kwargs_ = dict(izip(self.__slots__, args))
kwargs_.update(kwargs)
for key, value in kwargs_.iteritems():
setattr(self, key, value)
setted.add(key)
for key in set(self.__slots__) - setted:
setattr(self, key, None)
class processinfo(SlotBase):
__slots__ = ('ppidinfo', 'child_pids', 'memory', 'create_time', 'cmdline',
'environ')
def default_if_catch(exception_clses, default=None):
def _default_if_catch(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except exception_clses:
return default
return wrapper
return _default_if_catch
class Process(object):
def __init__(self, pid, top_paths=None):
self.pid = pid
self.top_paths = top_paths
self.process = psutil.Process(pid=pid)
self._tree_string = ''
def cmdline(self):
try:
return self.process.cmdline()[0]
except Exception as e: