本文整理汇总了Python中psutil.wait_procs方法的典型用法代码示例。如果您正苦于以下问题:Python psutil.wait_procs方法的具体用法?Python psutil.wait_procs怎么用?Python psutil.wait_procs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类psutil
的用法示例。
在下文中一共展示了psutil.wait_procs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kill_process_tree
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def kill_process_tree(pid, sig=signal.SIGTERM, include_parent=True,
timeout=None, on_terminate=None):
"""Kill a process tree (including grandchildren) with signal
"sig" and return a (gone, still_alive) tuple.
"on_terminate", if specified, is a callabck function which is
called as soon as a child terminates.
"""
if pid == os.getpid():
raise RuntimeError("I refuse to kill myself")
parent = psutil.Process(pid)
children = parent.children(recursive=True)
if include_parent:
children.append(parent)
for p in children:
p.send_signal(sig)
gone, alive = psutil.wait_procs(children, timeout=timeout,
callback=on_terminate)
return (gone, alive)
示例2: killProcTree
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def killProcTree(pid=None, including_parent=True):
if not pid:
# kill the process itself
pid = os.getpid()
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
##############################################################################
# Run hashdeep on input file or directory, and output filename to digest mapping
##############################################################################
示例3: killProcTree
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def killProcTree(pid=None, including_parent=True):
if not pid:
# kill the process itself
pid = os.getpid()
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
###########################################################
# ProgressBar
###########################################################
示例4: reap
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def reap(process, timeout=3):
"Tries hard to terminate and ultimately kill all the children of this process."
def on_terminate(proc):
pass
# print("process {} terminated with exit code {}".format(proc.pid, proc.returncode))
try:
procs = process.children(recursive=True)
# send SIGTERM
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs, timeout=timeout, callback=on_terminate)
if alive:
# send SIGKILL
for p in alive:
p.kill()
gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate)
if alive:
# give up
for p in alive:
print("process {} survived SIGKILL; giving up" % p.pid)
process.kill()
except:
print("Killing failed; assuming process exited early.")
示例5: reap_children
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def reap_children(timeout=3):
"Tries hard to terminate and ultimately kill all the children of this process."
def on_terminate(proc):
pass
# print("process {} terminated with exit code {}".format(proc, proc.returncode))
procs = psutil.Process().children(recursive=True)
# send SIGTERM
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs, timeout=timeout, callback=on_terminate)
if alive:
# send SIGKILL
for p in alive:
# print("process {} survived SIGTERM; trying SIGKILL" % p.pid)
p.kill()
gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate)
if alive:
# give up
for p in alive:
print("process {} survived SIGKILL; giving up" % p.pid)
示例6: kill_proc
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def kill_proc(proc):
"""
Kill a process and its children processes
:param proc: Process class defined in psutil
:return: None
"""
try:
children = proc.children()
for child in children:
try:
child.terminate()
except:
pass
gone, still_alive = psutil.wait_procs(children, timeout=3)
for p in still_alive:
p.kill()
proc.kill()
except:
pass
示例7: test_recursive_terminate
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def test_recursive_terminate(use_psutil):
event = ctx_loky.Event()
p = ctx_loky.Process(target=_run_nested_delayed, args=(4, 1000, event))
p.start()
# Wait for all the processes to be launched
if not event.wait(30):
recursive_terminate(p, use_psutil=use_psutil)
raise RuntimeError("test_recursive_terminate was not able to launch "
"all nested processes.")
children = psutil.Process(pid=p.pid).children(recursive=True)
recursive_terminate(p, use_psutil=use_psutil)
# The process can take some time finishing so we should wait up to 5s
gone, alive = psutil.wait_procs(children, timeout=5)
msg = "Should be no descendant left but found:\n{}"
assert len(alive) == 0, msg.format(alive)
示例8: kill_proc_tree
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,
timeout=None, on_terminate=None):
"""Kill a process tree (including grandchildren) with signal
"sig" and return a (gone, still_alive) tuple.
"on_terminate", if specified, is a callabck function which is
called as soon as a child terminates.
"""
if pid == os.getpid():
raise RuntimeError("I refuse to kill myself")
parent = psutil.Process(pid)
children = parent.children(recursive=True)
if include_parent:
children.append(parent)
for p in children:
p.send_signal(sig)
gone, alive = psutil.wait_procs(children, timeout=timeout,
callback=on_terminate)
return (gone, alive)
示例9: die_die_die
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def die_die_die(self: "Helpers", parent: ProcessType) -> None:
if isinstance(parent, int):
try:
par = Process(parent)
except NoSuchProcess:
self.warn("Unable to kill process id:{}".format(parent))
return
else:
par = parent
for child_process in par.children():
self.die_die_die(child_process)
if child_process.is_running():
child_process.terminate()
_, alive = wait_procs([child_process], timeout=3)
for p in alive:
p.kill()
示例10: kill_all_subprocesses
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def kill_all_subprocesses(pid=None, include_self=False):
# kill all subprocesses that could have been spawn from the current process
try:
proc = psutil.Process(pid)
except Exception:
# if no such process
return
procs = proc.children(recursive=True) + ([proc] if include_self else [])
if not procs:
return
for p in procs:
p.terminate()
alive = psutil.wait_procs(procs, timeout=3)[-1]
if alive:
for p in alive:
p.kill()
alive = psutil.wait_procs(procs, timeout=3)[-1]
if alive:
for p in alive:
env.logger.warning(f'Failed to kill subprocess {p.pid}')
示例11: close
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def close(self):
"""Close SSH tunnel."""
logging.debug('Closing tunnel "%s"', self.context.uri)
if self._closed:
return
# Find all ssh instances for user with uri tunnel the hard way...
targets = [
p
for p in psutil.process_iter(attrs=['name', 'username', 'cmdline'])
if p.info['username'] == getpass.getuser()
and p.info['name'] == 'ssh'
and self.context.local_socket in ' '.join(p.info['cmdline'])
] # yapf: disable
# ask nicely for ssh to quit, reap results
for proc in targets:
proc.terminate()
_, alive = psutil.wait_procs(targets, timeout=300)
# kill off the uncooperative, then report any stragglers
for proc in alive:
proc.kill()
_, alive = psutil.wait_procs(targets, timeout=300)
for proc in alive:
logging.info('process %d survived SIGKILL, giving up.', proc.pid)
with suppress(OSError):
os.remove(self.context.local_socket)
self._closed = True
示例12: wait_procs
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def wait_procs(*args, **kwargs):
return psutil.wait_procs(*args, **kwargs)
示例13: test_wait_procs_no_timeout
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def test_wait_procs_no_timeout(self):
sproc1 = get_test_subprocess()
sproc2 = get_test_subprocess()
sproc3 = get_test_subprocess()
procs = [psutil.Process(x.pid) for x in (sproc1, sproc2, sproc3)]
for p in procs:
p.terminate()
gone, alive = psutil.wait_procs(procs)
示例14: killProcTree
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def killProcTree(pid=None, including_parent=True):
if not pid:
# kill the process itself
pid = os.getpid()
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
示例15: reap_children
# 需要导入模块: import psutil [as 别名]
# 或者: from psutil import wait_procs [as 别名]
def reap_children(recursive=False):
"""Terminate and wait() any subprocess started by this test suite
and any children currently running, ensuring that no processes stick
around to hog resources.
If resursive is True it also tries to terminate and wait()
all grandchildren started by this process.
"""
# Get the children here before terminating them, as in case of
# recursive=True we don't want to lose the intermediate reference
# pointing to the grandchildren.
children = psutil.Process().children(recursive=recursive)
# Terminate subprocess.Popen.
while _subprocesses_started:
subp = _subprocesses_started.pop()
terminate(subp)
# Collect started pids.
while _pids_started:
pid = _pids_started.pop()
terminate(pid)
# Terminate children.
if children:
for p in children:
terminate(p, wait_timeout=None)
gone, alive = psutil.wait_procs(children, timeout=GLOBAL_TIMEOUT)
for p in alive:
warn("couldn't terminate process %r; attempting kill()" % p)
terminate(p, sig=signal.SIGKILL)
# ===================================================================
# --- OS
# ===================================================================