Python psutil.Process() Examples

The following are 30 code examples of psutil.Process(). 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: _common.py    From ALF with Apache License 2.0 8 votes vote down vote up
def __init__(self, pid, idle_limit=None, memory_limit=None, time_limit=None):
        self.limit = {"idle":idle_limit,
                      "memory":memory_limit,
                      "time":time_limit}
        self.check = {"idle":0,
                      "memory":0,
                      "time":0}
        self.idle_start = None
        try:
            if isinstance(pid, psutil.Process):
                self.ps = pid
            else:
                self.ps = psutil.Process(pid)
            self.ps.get_cpu_percent(interval=0)
        except psutil.NoSuchProcess:
            self.ps = None 
Example #2
Source File: process.py    From tenpy with GNU General Public License v3.0 7 votes vote down vote up
def memory_usage():
    """Return memory usage of the running python process.

    You can ``pip install psutil`` if you get only ``-1.``.

    Returns
    -------
    mem : float
        Currently used memory in megabytes. ``-1.`` if no way to read out.
    """
    try:
        import resource  # linux-only
        return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
    except ImportError:
        pass
    try:
        import psutil
        proc = psutil.Process()
        return proc.memory_info().rss / 1024**2
    except ImportError:
        pass
    warnings.warn("No tool to determine memory_usage")
    return -1. 
Example #3
Source File: dtest.py    From cassandra-dtest with Apache License 2.0 6 votes vote down vote up
def kill_windows_cassandra_procs():
    # On Windows, forcefully terminate any leftover previously running cassandra processes. This is a temporary
    # workaround until we can determine the cause of intermittent hung-open tests and file-handles.
    if is_win():
        try:
            import psutil
            for proc in psutil.process_iter():
                try:
                    pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline'])
                except psutil.NoSuchProcess:
                    pass
                else:
                    if (pinfo['name'] == 'java.exe' and '-Dcassandra' in pinfo['cmdline']):
                        print('Found running cassandra process with pid: ' + str(pinfo['pid']) + '. Killing.')
                        psutil.Process(pinfo['pid']).kill()
        except ImportError:
            logger.debug("WARN: psutil not installed. Cannot detect and kill "
                  "running cassandra processes - you may see cascading dtest failures.") 
Example #4
Source File: WinDBGMemoryLimit.py    From ALF with Apache License 2.0 6 votes vote down vote up
def get_mem_usage(pid):
    try:
        proc = psutil.Process(pid)
        tmp_val = proc.get_memory_info()[0]
    except psutil.NoSuchProcess:
        tmp_val = 0
    return tmp_val 
Example #5
Source File: Collection.py    From fullrmc with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_memory_usage():
    """
    Get current process memory usage. This is method requires
    psutils to be installed.

    :Returns:
        #. memory (float, None): The memory usage in Megabytes.
           When psutils is not installed, None is returned.
    """
    try:
        import psutil
        process = psutil.Process(os.getpid())
        memory  = float( process.memory_info()[0] ) / float(2 ** 20)
    except:
        LOGGER.warn("memory usage cannot be profiled. psutil is not installed. pip install psutil")
        memory = None
    return memory 
Example #6
Source File: plugin.py    From pytest-mp with MIT License 6 votes vote down vote up
def submit_batch_to_process(batch, session):

    def run_batch(tests, finished_signal):
        for i, test in enumerate(tests):
            next_test = tests[i + 1] if i + 1 < len(tests) else None
            test.config.hook.pytest_runtest_protocol(item=test, nextitem=next_test)
            if session.shouldstop:
                raise session.Interrupted(session.shouldstop)
        finished_signal.set()

    proc = multiprocessing.Process(target=run_batch, args=(batch['tests'], synchronization['trigger_process_loop']))
    with synchronization['processes_lock']:
        proc.start()
        pid = proc.pid
        synchronization['running_pids'][pid] = True
        synchronization['processes'][pid] = proc
    synchronization['trigger_process_loop'].set() 
Example #7
Source File: WinDBGMemoryLimit.py    From ALF with Apache License 2.0 6 votes vote down vote up
def get_proc_run_time():
    #Debug session time: Tue Aug 21 16:27:31.971 2012 (UTC - 4:00)
    #System Uptime: 5 days 13:06:34.062
    #Process Uptime: 0 days 0:00:02.718
      #Kernel time: 0 days 0:00:00.000
      #User time: 0 days 0:00:00.000
    duration = 0
    for line in pykd.dbgCommand(".time").splitlines()[-2:]:
        line = line.strip().split()
        duration += int(line[2]) * 86400 # days
        line = line[-1].split('.')
        duration += float("0.%s" % line[-1])
        line = line[0].split(':')
        duration += int(line[0]) * 3600 # hours
        duration += int(line[1]) * 60 # minutes
        duration += int(line[2]) # seconds
    return duration 
Example #8
Source File: test_issue_569.py    From python-slackclient with MIT License 6 votes vote down vote up
def setUp(self):
        if not hasattr(self, "logger"):
            self.logger = logging.getLogger(__name__)
            self.channel_id = os.environ[SLACK_SDK_TEST_RTM_TEST_CHANNEL_ID]
            self.bot_token = os.environ[SLACK_SDK_TEST_CLASSIC_APP_BOT_TOKEN]

        if not hasattr(self, "cpu_monitor") or not TestRTMClient.cpu_monitor.is_alive():
            def run_cpu_monitor(self):
                self.logger.debug("Starting CPU monitor in another thread...")
                TestRTMClient.cpu_usage = 0
                while True:
                    p = psutil.Process(os.getpid())
                    current_cpu_usage: float = p.cpu_percent(interval=0.5)
                    self.logger.debug(current_cpu_usage)
                    if current_cpu_usage > TestRTMClient.cpu_usage:
                        TestRTMClient.cpu_usage = current_cpu_usage

            TestRTMClient.cpu_monitor = threading.Thread(target=run_cpu_monitor, args=[self])
            TestRTMClient.cpu_monitor.setDaemon(True)
            TestRTMClient.cpu_monitor.start()

        self.rtm_client = None
        self.web_client = None 
Example #9
Source File: endpoint.py    From funcX with Apache License 2.0 6 votes vote down vote up
def check_pidfile(filepath, match_name, endpoint_name):
    """ Helper function to identify possible dead endpoints
    """
    if not os.path.exists(filepath):
        return

    older_pid = int(open(filepath, 'r').read().strip())

    try:
        proc = psutil.Process(older_pid)
        if proc.name() == match_name:
            logger.info("Endpoint is already active")
    except psutil.NoSuchProcess:
        logger.info("A prior Endpoint instance appears to have been terminated without proper cleanup")
        logger.info('''Please cleanup using:
    $ funcx-endpoint stop {}'''.format(endpoint_name)) 
Example #10
Source File: conftest.py    From FARM with Apache License 2.0 6 votes vote down vote up
def adaptive_model_qa(use_gpu, num_processes):
    """
    PyTest Fixture for a Question Answering Inferencer based on PyTorch.
    """
    try:
        model = Inferencer.load(
            "deepset/bert-base-cased-squad2",
            task_type="question_answering",
            batch_size=16,
            num_processes=num_processes,
            gpu=use_gpu,
        )
        yield model
    finally:
        if num_processes != 0:
            # close the pool
            # we pass join=True to wait for all sub processes to close
            # this is because below we want to test if all sub-processes
            # have exited
            model.close_multiprocessing_pool(join=True)

    # check if all workers (sub processes) are closed
    current_process = psutil.Process()
    children = current_process.children()
    assert len(children) == 0 
Example #11
Source File: utils.py    From mars with Apache License 2.0 6 votes vote down vote up
def kill_process_tree(pid, include_parent=True):
    try:
        import psutil
    except ImportError:  # pragma: no cover
        return
    try:
        proc = psutil.Process(pid)
    except psutil.NoSuchProcess:
        return

    plasma_sock_dir = None
    children = proc.children(recursive=True)
    if include_parent:
        children.append(proc)
    for p in children:
        try:
            if 'plasma' in p.name():
                plasma_sock_dir = next((conn.laddr for conn in p.connections('unix')
                                        if 'plasma' in conn.laddr), None)
            p.kill()
        except psutil.NoSuchProcess:  # pragma: no cover
            pass
    if plasma_sock_dir:
        shutil.rmtree(plasma_sock_dir, ignore_errors=True) 
Example #12
Source File: cluster.py    From pyquarkchain with MIT License 6 votes vote down vote up
def kill_child_processes(parent_pid):
    """ Kill all the subprocesses recursively """
    try:
        parent = psutil.Process(parent_pid)
    except psutil.NoSuchProcess:
        return
    children = parent.children(recursive=True)
    print(
        "================================ SHUTTING DOWN CLUSTER ================================"
    )
    for process in children:
        try:
            print("SIGTERM >>> " + " ".join(process.cmdline()[1:]))
        except Exception:
            pass
        process.send_signal(signal.SIGTERM)
        process.wait() 
Example #13
Source File: haproxy_synapse_reaper.py    From synapse-tools with Apache License 2.0 6 votes vote down vote up
def get_alumni(
    username: str,
) -> Iterator[psutil.Process]:
    main_pid = get_main_pid()

    for proc in psutil.process_iter():
        if proc.name() != 'haproxy-synapse':
            continue

        if proc.username() != username:
            continue

        if proc.pid == main_pid:
            continue

        yield proc 
Example #14
Source File: haproxy_synapse_reaper.py    From synapse-tools with Apache License 2.0 6 votes vote down vote up
def remove_stale_alumni_pidfiles(
    alumni: Iterable[psutil.Process],
    state_dir: str,
) -> None:
    alumni_pids = [proc.pid for proc in alumni]

    for pidfile in os.listdir(state_dir):
        try:
            pid = int(pidfile)
        except ValueError:
            log.warn('Ignoring invalid filename: %s' % pidfile)
            continue

        if pid in alumni_pids:
            continue

        log.info('Removing stale pidfile for %d', pid)
        os.remove(os.path.join(state_dir, pidfile)) 
Example #15
Source File: __init__.py    From ACE with Apache License 2.0 6 votes vote down vote up
def start_engine(self):

        self.engine_process = Process(target=self.engine_loop, name='ACE Engine')
        self.engine_process.start()
        
        # wait for the message from the child that it started up
        try:
            logging.debug("waiting for engine to start...")
            self.engine_startup_event.wait()
            logging.info("engine started")

        except Exception as e:
            logging.error("engine failed to start: {}".format(e))
            return False

        return True 
Example #16
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 #17
Source File: quotas.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def wrap_session(session, cpu_quota=None, load_quota=None):
    """Wraps the session limiting cpu quota."""
    if load_quota is None:
        load_quota = session.GetParameter("load_quota")

    if cpu_quota is None:
        cpu_quota = session.GetParameter("cpu_quota")

    if cpu_quota == None and load_quota == None:
        return session

    # Store the process's current CPU utilization.
    proc = psutil.Process()
    cpu_times = proc.cpu_times()
    start_time = cpu_times.user + cpu_times.system
    state = dict(last=time.time(),
                 start_time=start_time,
                 proc=proc)

    def quota_callback(*_, **__):
        check_quota(state, cpu_quota, load_quota)

    # Register our progress dispatcher.
    session.progress.Register("quota", quota_callback)
    return session 
Example #18
Source File: ctprobe_container_crawler.py    From agentless-system-crawler with Apache License 2.0 6 votes vote down vote up
def check_ctprobe_alive(self, pid):
        """
          Check whether the conntrackprobe with the given PID is still running
          Returns True if the conntrackprobe is still alive, false otherwise.
        """
        gone = False
        try:
            proc = psutil.Process(pid=pid)
            if not proc or proc.name() != 'conntrackprobe':
                gone = True
        except Exception:
            gone = True

        if gone:
            CTProbeContainerCrawler.ifaces_monitored = []
        return not gone 
Example #19
Source File: jobs.py    From minemeld-core with Apache License 2.0 6 votes vote down vote up
def _get_job_status(self, jobpid, jobhash):
        try:
            jobprocess = psutil.Process(pid=jobpid)

        except psutil.NoSuchProcess:
            return {
                'status': 'DONE',
                'returncode': None
            }

        if hash(jobprocess) != jobhash:
            return {
                'status': 'DONE',
                'returncode': None
            }

        return {
            'status': 'RUNNING'
        } 
Example #20
Source File: driver.py    From info-flow-experiments with GNU General Public License v3.0 5 votes vote down vote up
def kill_proc_tree(pid, including_parent=True):    
    parent = psutil.Process(pid)
    for child in parent.get_children(recursive=True):
        child.kill()
    if including_parent:
        parent.kill() 
Example #21
Source File: rpc.py    From brownie with MIT License 5 votes vote down vote up
def _check_connections(proc: psutil.Process, laddr: Tuple) -> bool:
    try:
        return laddr in [i.laddr for i in proc.connections()]
    except psutil.AccessDenied:
        return False
    except psutil.ZombieProcess:
        return False
    except psutil.NoSuchProcess:
        return False 
Example #22
Source File: agent.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(ResourcesImpl, self).__init__(*args, **kwargs)
        self._proc = psutil.Process() 
Example #23
Source File: agent.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def process_flows(self, flows):
        """Process all the flows and report the number that ran."""
        self.sessions_cache = {}
        flows_ran = 0
        try:
            for flow_obj in flows:
                if not isinstance(flow_obj, agent.Flow):
                    continue

                # We already did this flow before.
                flows_ran += 1
                now = time.time()
                self.writeback.current_flow = flow_obj
                self.writeback.last_flow_time = now
                self.writeback.current_flow.status.timestamp = now

                # Sync the writeback in case we crash.
                self._config.client.save_writeback()

                # Start counting resources from now.
                self._quota = flow_obj.quota
                self._quota.start()

                try:
                    for status in self.session.plugins.run_flow(flow_obj):
                        self.session.logging.debug("Status: %s", status)
                finally:
                    # We ran the flow with no exception - remove
                    # transaction.
                    self.writeback.current_flow = None
                    self._config.client.save_writeback()

        finally:
            # Stop measuring quotas.
            self._quota = None

        return flows_ran 
Example #24
Source File: unicorn_binance_websocket_api_manager.py    From unicorn-binance-websocket-api with MIT License 5 votes vote down vote up
def get_process_usage_memory(self):
        """
        Get the used memory of this process

        :return: str
        """
        process = psutil.Process(os.getpid())
        memory = self.get_human_bytesize(process.memory_info()[0])
        return memory 
Example #25
Source File: core.py    From pandoc-xnos with GNU General Public License v3.0 5 votes vote down vote up
def get_meta(meta, name):
    """Retrieves the metadata variable `name` from the `meta` dict."""
    assert name in meta
    data = meta[name]

    if data['t'] in ['MetaString', 'MetaBool']:
        return data['c']
    if data['t'] == 'MetaInlines':
        # Handle bug in pandoc 2.2.3 and 2.2.3.1: Return boolean value rather
        # than strings, as appropriate.
        if len(data['c']) == 1 and data['c'][0]['t'] == 'Str':
            if data['c'][0]['c'] in ['true', 'True', 'TRUE']:
                return True
            if data['c'][0]['c'] in ['false', 'False', 'FALSE']:
                return False
        return stringify(data['c'])
    if data['t'] == 'MetaList':
        try:  # Process MetaList of MetaMaps
            ret = []
            for v in data['c']:
                assert v['t'] == 'MetaMap'
                entry = {}
                for key in v['c']:
                    entry[key] = stringify(v['c'][key])
                ret.append(entry)
            return ret
        except AssertionError:
            pass
        return [stringify(v['c']) for v in data['c']]
    if data['t'] == 'MetaMap':
        ret = {}
        for key in data['c']:
            ret[key] = stringify(data['c'][key])
        return ret
    raise RuntimeError("Could not understand metadata variable '%s'." % name)


# elt() ---------------------------------------------------------------------- 
Example #26
Source File: core.py    From pandoc-xnos with GNU General Public License v3.0 5 votes vote down vote up
def _join_strings(x, start=0):
    """Joins adjacent Str elements found in the element list `x`."""
    for i in range(start, len(x)-1):  # Process successive pairs of elements
        if x[i]['t'] == 'Str' and x[i+1]['t'] == 'Str':
            x[i]['c'] += x[i+1]['c']
            del x[i+1]  # In-place deletion of element from list
            return None  # Forces processing to repeat
    return True  # Terminates processing

# pylint: disable=unused-argument 
Example #27
Source File: threads.py    From vscode-esp-idf-extension with Apache License 2.0 5 votes vote down vote up
def kill_outer_proc(name):
        pid_list = psutil.pids()
        for pid in pid_list:
            p = psutil.Process(pid)
            if p.name() == name:
                p.kill() 
Example #28
Source File: memory_profiler.py    From vprof with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, target_modules):
        self._events_list = deque()
        self._original_trace_function = sys.gettrace()
        self._process = psutil.Process(os.getpid())
        self._resulting_events = []
        self.mem_overhead = None
        self.target_modules = target_modules 
Example #29
Source File: system_status.py    From Paradrop with Apache License 2.0 5 votes vote down vote up
def getProcessInfo(cls, pid):
        proc = psutil.Process(pid=pid)
        with proc.oneshot():
            proc_info = {
                'cpu_num': proc.cpu_num(),
                'cpu_times': proc.cpu_times().__dict__,
                'create_time': proc.create_time(),
                'memory_info': proc.memory_info().__dict__,
                'num_ctx_switches': proc.num_ctx_switches().__dict__,
                'num_threads': proc.num_threads()
            }
        return proc_info 
Example #30
Source File: processes.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def __repr__(self):
        return "<Live Process pid=%s>" % self.pid