本文整理汇总了Python中psutil.Popen.get_children方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.get_children方法的具体用法?Python Popen.get_children怎么用?Python Popen.get_children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil.Popen
的用法示例。
在下文中一共展示了Popen.get_children方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import get_children [as 别名]
#.........这里部分代码省略.........
self._worker.stderr.close()
if self._worker.stdout is not None:
self._worker.stdout.close()
except NoSuchProcess:
pass
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
def info(self):
"""Return process info.
The info returned is a mapping with these keys:
- **mem_info1**: Resident Set Size Memory in bytes (RSS)
- **mem_info2**: Virtual Memory Size in bytes (VMS).
- **cpu**: % of cpu usage.
- **mem**: % of memory usage.
- **ctime**: process CPU (user + system) time in seconds.
- **pid**: process id.
- **username**: user name that owns the process.
- **nice**: process niceness (between -20 and 20)
- **cmdline**: the command line the process was run with.
"""
try:
info = get_info(self._worker)
except NoSuchProcess:
return "No such process (stopped?)"
info["age"] = self.age()
info["started"] = self.started
info["children"] = []
for child in self._worker.get_children():
info["children"].append(get_info(child))
return info
def children(self):
"""Return a list of children pids."""
return [child.pid for child in self._worker.get_children()]
def is_child(self, pid):
"""Return True is the given *pid* is a child of that process."""
pids = [child.pid for child in self._worker.get_children()]
if pid in pids:
return True
return False
@debuglog
def send_signal_child(self, pid, signum):
"""Send signal *signum* to child *pid*."""
children = dict([(child.pid, child)
for child in self._worker.get_children()])
children[pid].send_signal(signum)
@debuglog
def send_signal_children(self, signum):
"""Send signal *signum* to all children."""
for child in self._worker.get_children():
try:
child.send_signal(signum)
except OSError as e:
if e.errno != errno.ESRCH:
raise
示例2: Fly
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import get_children [as 别名]
class Fly(object):
def __init__(self, wid, cmd, working_dir, shell, uid=None, gid=None, env=None):
self.wid = wid
self.working_dir = working_dir
self.shell = shell
self.env = env
self.cmd = cmd.replace("$WID", str(self.wid))
self.uid = to_uid(uid)
self.gid = to_gid(gid)
def preexec_fn():
os.setsid()
if self.gid:
try:
os.setgid(self.gid)
except OverflowError:
if not ctypes:
raise
# versions of python < 2.6.2 don't manage unsigned int for
# groups like on osx or fedora
os.setgid(-ctypes.c_int(-self.gid).value)
if self.uid:
os.setuid(self.uid)
self._worker = Popen(
self.cmd.split(),
cwd=self.working_dir,
shell=self.shell,
preexec_fn=preexec_fn,
env=self.env,
close_fds=True,
)
self.started = time.time()
def poll(self):
return self._worker.poll()
def send_signal(self, sig):
return self._worker.send_signal(sig)
def stop(self):
if self._worker.poll() is None:
return self._worker.terminate()
def age(self):
return time.time() - self.started
def info(self):
""" return process info """
info = _INFOLINE % get_info(self._worker)
lines = ["%s: %s" % (self.wid, info)]
for child in self._worker.get_children():
info = _INFOLINE % get_info(child)
lines.append(" %s" % info)
return "\n".join(lines)
def children(self):
return ",".join(["%s" % child.pid for child in self._worker.get_children()])
def send_signal_child(self, pid, signum):
pids = [child.pid for child in self._worker.get_children()]
if pid in pids:
child.send_signal(signum)
return "ok"
else:
return "error: child not found"
def send_signal_children(self, signum):
for child in self._worker.get_children():
child.send_signal(signum)
return "ok"
@property
def pid(self):
return self._worker.pid
示例3: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import get_children [as 别名]
#.........这里部分代码省略.........
def stop(self):
"""Terminate the process."""
try:
if self._worker.poll() is None:
return self._worker.terminate()
finally:
self._worker.stderr.close()
self._worker.stdout.close()
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
def info(self):
"""Return process info.
The info returned is a mapping with these keys:
- **mem_info1**: Resident Set Size Memory in bytes (RSS)
- **mem_info2**: Virtual Memory Size in bytes (VMS).
- **cpu**: % of cpu usage.
- **mem**: % of memory usage.
- **ctime**: process CPU (user + system) time in seconds.
- **pid**: process id.
- **username**: user name that owns the process.
- **nice**: process niceness (between -20 and 20)
- **cmdline**: the command line the process was run with.
"""
try:
info = get_info(self._worker)
except NoSuchProcess:
return "No such process (stopped?)"
info["children"] = []
for child in self._worker.get_children():
info["children"].append(get_info(child))
return info
def children(self):
"""Return a list of children pids."""
return [child.pid for child in self._worker.get_children()]
def is_child(self, pid):
"""Return True is the given *pid* is a child of that process."""
pids = [child.pid for child in self._worker.get_children()]
if pid in pids:
return True
return False
@debuglog
def send_signal_child(self, pid, signum):
"""Send signal *signum* to child *pid*."""
children = dict([(child.pid, child) \
for child in self._worker.get_children()])
children[pid].send_signal(signum)
@debuglog
def send_signal_children(self, signum):
"""Send signal *signum* to all children."""
for child in self._worker.get_children():
try:
child.send_signal(signum)
except OSError as e:
if e.errno != errno.ESRCH:
raise
@property
def status(self):
"""Return the process status as a constant
- RUNNING
- DEAD_OR_ZOMBIE
- OTHER
"""
try:
if self._worker.status in (STATUS_ZOMBIE, STATUS_DEAD):
return DEAD_OR_ZOMBIE
except NoSuchProcess:
return OTHER
if self._worker.is_running():
return RUNNING
return OTHER
@property
def pid(self):
"""Return the *pid*"""
return self._worker.pid
@property
def stdout(self):
"""Return the *stdout* stream"""
return self._worker.stdout
@property
def stderr(self):
"""Return the *stdout* stream"""
return self._worker.stderr
示例4: Process
# 需要导入模块: from psutil import Popen [as 别名]
# 或者: from psutil.Popen import get_children [as 别名]
#.........这里部分代码省略.........
"""Sends a signal **sig** to the process."""
return self._worker.send_signal(sig)
@debuglog
def stop(self):
"""Terminate the process."""
if self._worker.poll() is None:
return self._worker.terminate()
def age(self):
"""Return the age of the process in seconds."""
return time.time() - self.started
def info(self):
"""Return process info.
The info returned is a mapping with these keys:
- **mem_info1**: Resident Set Size Memory in bytes (RSS)
- **mem_info2**: Virtual Memory Size in bytes (VMS).
- **cpu**: % of cpu usage.
- **mem**: % of memory usage.
- **ctime**: process CPU (user + system) time in seconds.
- **pid**: process id.
- **username**: user name that owns the process.
- **nice**: process niceness (between -20 and 20)
- **cmdline**: the command line the process was run with.
"""
try:
info = get_info(self._worker)
except NoSuchProcess:
return "No such process (stopped?)"
info["children"] = []
for child in self._worker.get_children():
info["children"].append(get_info(child))
return info
def children(self):
"""Return a list of children pids."""
return [child.pid for child in self._worker.get_children()]
def is_child(self, pid):
"""Return True is the given *pid* is a child of that process."""
pids = [child.pid for child in self._worker.get_children()]
if pid in pids:
return True
return False
@debuglog
def send_signal_child(self, pid, signum):
"""Send signal *signum* to child *pid*."""
children = dict([(child.pid, child) \
for child in self._worker.get_children()])
children[pid].send_signal(signum)
@debuglog
def send_signal_children(self, signum):
"""Send signal *signum* to all children."""
for child in self._worker.get_children():
try:
child.send_signal(signum)
except OSError as e:
if e.errno != errno.ESRCH:
raise
@property
def status(self):
"""Return the process status as a constant
- RUNNING
- DEAD_OR_ZOMBIE
- OTHER
"""
try:
if self._worker.status in (STATUS_ZOMBIE, STATUS_DEAD):
return DEAD_OR_ZOMBIE
except NoSuchProcess:
return OTHER
if self._worker.is_running():
return RUNNING
return OTHER
@property
def pid(self):
"""Return the *pid*"""
return self._worker.pid
@property
def stdout(self):
"""Return the *stdout* stream"""
return self._worker.stdout
@property
def stderr(self):
"""Return the *stdout* stream"""
return self._worker.stderr