Python psutil.wait_procs() Examples

The following are 30 code examples of psutil.wait_procs(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module psutil , or try the search function .
Example #1
Source File: test_loky_backend.py    From loky with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #2
Source File: Demo_psutil_Kill_Processes.py    From PySimpleGUI with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #3
Source File: bioqueue.py    From BioQueue with Apache License 2.0 6 votes vote down vote up
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 
Example #4
Source File: gui.py    From bc18-scaffold with MIT License 6 votes vote down vote up
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) 
Example #5
Source File: player_plain.py    From bc18-scaffold with MIT License 6 votes vote down vote up
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.") 
Example #6
Source File: Demo_psutil_Kill_Processes.py    From PySimpleGUI with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #7
Source File: Web_psutil_Kill_Processes.py    From PySimpleGUI with GNU Lesser General Public License v3.0 6 votes vote down vote up
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) 
Example #8
Source File: __init__.py    From robotframework-seleniumtestability with Apache License 2.0 6 votes vote down vote up
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() 
Example #9
Source File: job_util.py    From osspolice with GNU General Public License v3.0 6 votes vote down vote up
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
########################################################### 
Example #10
Source File: job_util.py    From osspolice with GNU General Public License v3.0 6 votes vote down vote up
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
############################################################################## 
Example #11
Source File: __init__.py    From ACE with Apache License 2.0 6 votes vote down vote up
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) 
Example #12
Source File: executor_utils.py    From sos with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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}') 
Example #13
Source File: _mpi.py    From sagemaker-containers with Apache License 2.0 5 votes vote down vote up
def _wait_orted_process_to_finish():  # type: () -> None
    """Placeholder docstring"""
    orted = _orted_process()
    psutil.wait_procs(orted) 
Example #14
Source File: conftest.py    From daemonocle with MIT License 5 votes vote down vote up
def teardown(self):
        procs = []
        for proc in psutil.process_iter():
            try:
                if (proc.exe() == sys.executable and
                        self.realpath in proc.cmdline()):
                    proc.terminate()
                    procs.append(proc)
            except (psutil.NoSuchProcess, psutil.AccessDenied, OSError):
                continue
        if psutil.wait_procs(procs, timeout=1)[1]:
            raise OSError('Failed to terminate subprocesses')
        shutil.rmtree(self.dirname) 
Example #15
Source File: __init__.py    From pytest-salt with Apache License 2.0 5 votes vote down vote up
def terminate_process_list(process_list, kill=False, slow_stop=False):

    def on_process_terminated(proc):
        log.info('Process %s terminated with exit code: %s', getattr(proc, '_cmdline', proc), proc.returncode)

    # Try to terminate processes with the provided kill and slow_stop parameters
    log.info('Terminating process list. 1st step. kill: %s, slow stop: %s', kill, slow_stop)

    # Remove duplicates from the process list
    seen_pids = []
    start_count = len(process_list)
    for proc in process_list[:]:
        if proc.pid in seen_pids:
            process_list.remove(proc)
        seen_pids.append(proc.pid)
    end_count = len(process_list)
    if end_count < start_count:
        log.debug('Removed %d duplicates from the initial process list', start_count - end_count)

    _terminate_process_list(process_list, kill=kill, slow_stop=slow_stop)
    psutil.wait_procs(process_list, timeout=15, callback=on_process_terminated)

    if process_list:
        # If there's still processes to be terminated, retry and kill them if slow_stop is False
        log.info('Terminating process list. 2nd step. kill: %s, slow stop: %s', slow_stop is False, slow_stop)
        _terminate_process_list(process_list, kill=slow_stop is False, slow_stop=slow_stop)
        psutil.wait_procs(process_list, timeout=10, callback=on_process_terminated)

    if process_list:
        # If there's still processes to be terminated, just kill them, no slow stopping now
        log.info('Terminating process list. 3rd step. kill: True, slow stop: False')
        _terminate_process_list(process_list, kill=True, slow_stop=False)
        psutil.wait_procs(process_list, timeout=5, callback=on_process_terminated)

    if process_list:
        # In there's still processes to be terminated, log a warning about it
        log.warning('Some processes failed to properly terminate: %s', process_list) 
Example #16
Source File: wrapper.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def terminate_all_children():
        """ Terminates all children """
        logger.debug("Terminating Process...")
        print("Terminating Process...", flush=True)
        children = psutil.Process().children(recursive=True)
        for child in children:
            child.terminate()
        _, alive = psutil.wait_procs(children, timeout=10)
        if not alive:
            logger.debug("Terminated")
            print("Terminated")
            return

        logger.debug("Termination timed out. Killing Process...")
        print("Termination timed out. Killing Process...", flush=True)
        for child in alive:
            child.kill()
        _, alive = psutil.wait_procs(alive, timeout=10)
        if not alive:
            logger.debug("Killed")
            print("Killed")
        else:
            for child in alive:
                msg = "Process {} survived SIGKILL. Giving up".format(child)
                logger.debug(msg)
                print(msg) 
Example #17
Source File: abort.py    From neptune-client with Apache License 2.0 5 votes vote down vote up
def abort(self):
        try:
            processes = self._get_process_with_children(psutil.Process(self._pid))
        except psutil.NoSuchProcess:
            processes = []

        for p in processes:
            self._abort(p)
        _, alive = psutil.wait_procs(processes, timeout=self.KILL_TIMEOUT)
        for p in alive:
            self._kill(p) 
Example #18
Source File: psutil.py    From pulseaudio-dlna with GNU General Public License v3.0 5 votes vote down vote up
def wait_procs(*args, **kwargs):
    return psutil.wait_procs(*args, **kwargs) 
Example #19
Source File: base.py    From simpleflow with MIT License 5 votes vote down vote up
def reap_process_tree(pid, wait_timeout=settings.ACTIVITY_SIGTERM_WAIT_SEC):
    """
    TERMinates (and KILLs) if necessary a process and its descendants.

    See also: https://psutil.readthedocs.io/en/latest/#kill-process-tree.

    :param pid: Process ID
    :type pid: int
    :param wait_timeout: Wait timeout
    :type wait_timeout: float
    """

    def on_terminate(p):
        logger.info('process: terminated pid={} retcode={}'.format(p.pid, p.returncode))

    if pid == os.getpid():
        raise RuntimeError('process: cannot terminate self!')
    parent = psutil.Process(pid)
    procs = parent.children(recursive=True)
    procs.append(parent)
    # Terminate
    for p in procs:
        try:
            p.terminate()
        except psutil.NoSuchProcess:
            pass
    _, alive = psutil.wait_procs(procs, timeout=wait_timeout, callback=on_terminate)
    # Kill
    for p in alive:
        logger.warning('process: pid={} status={} did not respond to SIGTERM. Trying SIGKILL'.format(p.pid, p.status()))
        try:
            p.kill()
        except psutil.NoSuchProcess:
            pass
    # Check
    _, alive = psutil.wait_procs(alive)
    for p in alive:
        logger.error('process: pid={} status={} still alive. Giving up!'.format(p.pid, p.status())) 
Example #20
Source File: thread.py    From pytest-plugins with MIT License 5 votes vote down vote up
def _kill_all(procs, sig):
    log.debug("Killing %d processes with signal %s" % (len(procs), sig))
    for p in procs:
        p.send_signal(sig)

    log.debug("Waiting for %d processes to die" % len(procs))
    gone, alive = psutil.wait_procs(procs, timeout=KILL_WAIT_SECS)

    if len(alive) == 0:
        log.debug("All processes are terminated")
        return

    log.warning("%d processes remainings: %s" % (len(alive), ",".join([p.name() for p in alive])))
    raise ProcessStillRunningException() 
Example #21
Source File: util.py    From guildai with Apache License 2.0 5 votes vote down vote up
def kill_process_tree(pid, force=False, timeout=None):
    import psutil
    import signal

    if force:
        sig = signal.SIGKILL
    else:
        sig = signal.SIGTERM
    root = psutil.Process(pid)
    procs = [root] + root.children(recursive=True)
    for proc in procs:
        proc.send_signal(sig)
    return psutil.wait_procs(procs, timeout=timeout) 
Example #22
Source File: postmaster.py    From patroni with MIT License 5 votes vote down vote up
def signal_kill(self):
        """to suspend and kill postmaster and all children

        :returns True if postmaster and children are killed, False if error
        """
        try:
            self.suspend()
        except psutil.NoSuchProcess:
            return True
        except psutil.Error as e:
            logger.warning('Failed to suspend postmaster: %s', e)

        try:
            children = self.children(recursive=True)
        except psutil.NoSuchProcess:
            return True
        except psutil.Error as e:
            logger.warning('Failed to get a list of postmaster children: %s', e)
            children = []

        try:
            self.kill()
        except psutil.NoSuchProcess:
            return True
        except psutil.Error as e:
            logger.warning('Could not kill postmaster: %s', e)
            return False

        for child in children:
            try:
                child.kill()
            except psutil.Error:
                pass
        psutil.wait_procs(children + [self])
        return True 
Example #23
Source File: postmaster.py    From patroni with MIT License 5 votes vote down vote up
def wait_for_user_backends_to_close(self):
        # These regexps are cross checked against versions PostgreSQL 9.1 .. 11
        aux_proc_re = re.compile("(?:postgres:)( .*:)? (?:(?:archiver|startup|autovacuum launcher|autovacuum worker|"
                                 "checkpointer|logger|stats collector|wal receiver|wal writer|writer)(?: process  )?|"
                                 "walreceiver|wal sender process|walsender|walwriter|background writer|"
                                 "logical replication launcher|logical replication worker for|bgworker:) ")

        try:
            children = self.children()
        except psutil.Error:
            return logger.debug('Failed to get list of postmaster children')

        user_backends = []
        user_backends_cmdlines = []
        for child in children:
            try:
                cmdline = child.cmdline()[0]
                if not aux_proc_re.match(cmdline):
                    user_backends.append(child)
                    user_backends_cmdlines.append(cmdline)
            except psutil.NoSuchProcess:
                pass
        if user_backends:
            logger.debug('Waiting for user backends %s to close', ', '.join(user_backends_cmdlines))
            psutil.wait_procs(user_backends)
        logger.debug("Backends closed") 
Example #24
Source File: cancellable.py    From patroni with MIT License 5 votes vote down vote up
def _kill_children(self):
        waitlist = []
        with self._lock:
            for child in self._process_children:
                try:
                    child.kill()
                except psutil.NoSuchProcess:
                    continue
                except psutil.AccessDenied as e:
                    logger.info('Failed to kill child process: %s', e.msg)
                waitlist.append(child)
        psutil.wait_procs(waitlist) 
Example #25
Source File: test_system.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
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) 
Example #26
Source File: carla.py    From Carla-RL with MIT License 5 votes vote down vote up
def kill_processes():

    binary = get_binary()

    # Iterate processes and terminate carla ones
    for process in psutil.process_iter():
        if process.name().lower().startswith(binary.split('.')[0].lower()):
            try:
                process.terminate()
            except:
                pass

    # Check if any are still alive, create a list
    still_alive = []
    for process in psutil.process_iter():
        if process.name().lower().startswith(binary.split('.')[0].lower()):
            still_alive.append(process)

    # Kill process and wait until it's being killed
    if len(still_alive):
        for process in still_alive:
            try:
                process.kill()
            except:
                pass
        psutil.wait_procs(still_alive)


# Starts Carla simulator 
Example #27
Source File: HappyProcess.py    From happy with Apache License 2.0 5 votes vote down vote up
def __wait_procs(self, procs, timeout):
        before = time.time()
        after = before

        alive = procs

        # (old versions of psutil have a bug and return too soon)
        while alive and (after - before) < timeout:
            next_timeout = math.ceil(timeout - (after - before))
            gone, alive = psutil.wait_procs(alive, timeout=next_timeout)
            after = time.time()
            if after < before:
                after = before
        return alive 
Example #28
Source File: test_drivers.py    From pifpaf with Apache License 2.0 5 votes vote down vote up
def _do_test_stuck(self, cmd):
        d = drivers.Driver(debug=True)
        d.setUp()
        c, _ = d._exec(cmd, wait_for_line="started")
        parent = psutil.Process(c.pid)
        procs = parent.children(recursive=True)
        procs.append(parent)
        d._kill(c)
        gone, alive = psutil.wait_procs(procs, timeout=0)
        self.assertEqual([], alive) 
Example #29
Source File: util.py    From pifpaf with Apache License 2.0 5 votes vote down vote up
def process_cleaner(parent):
    do_sigkill = False
    # NOTE(sileht): Add processes from process tree and process group
    # Relying on process tree only will not work in case of
    # parent dying prematuraly and double fork
    # Relying on process group only will not work in case of
    # subprocess calling again setsid()
    procs = set(_get_procs_of_pgid(parent.pid))
    try:
        LOG.debug("Terminating %s (%s)",
                  " ".join(parent.cmdline()), parent.pid)
        procs |= set(parent.children(recursive=True))
        procs.add(parent)
        parent.terminate()
    except psutil.NoSuchProcess:
        LOG.warning("`%s` is already gone, sending SIGKILL to its process "
                    "group", parent)
        do_sigkill = True
    else:
        # Waiting for all processes to stop
        for p in procs:
            try:
                LOG.debug("Waiting %s (%s)", " ".join(p.cmdline()), p.pid)
            except psutil.NoSuchProcess:
                pass
        gone, alive = psutil.wait_procs(procs, timeout=10)
        if alive:
            do_sigkill = True
            LOG.warning("`%s` didn't terminate cleanly after 10 seconds, "
                        "sending SIGKILL to its process group", parent)

    if do_sigkill and procs:
        for p in procs:
            try:
                LOG.debug("Killing %s (%s)", " ".join(p.cmdline()), p.pid)
                p.kill()
            except psutil.NoSuchProcess:
                pass
        gone, alive = psutil.wait_procs(procs, timeout=10)
        if alive:
            LOG.warning("`%s` survive SIGKILL", alive) 
Example #30
Source File: core.py    From Retropie-CRT-Edition with GNU General Public License v3.0 5 votes vote down vote up
def runcommand_kill(self, including_parent=False):
        """ kill runcommand and child processes if configuration is wrong"""
        p_iSelfPID = os.getpid()
        parent = psutil.Process(p_iSelfPID)
        children = parent.children(recursive=True)
        logging.info("INFO: killing all runcommand processes")
        for child in children:
            child.kill()
        gone, still_alive = psutil.wait_procs(children, timeout=5)
        if including_parent:
            parent.kill()
            parent.wait(5)