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 |
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 |
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: WinDBGMemoryLimit.py From ALF with Apache License 2.0 | 6 votes |
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 #4
Source File: WinDBGMemoryLimit.py From ALF with Apache License 2.0 | 6 votes |
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 |
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 |
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: dtest.py From cassandra-dtest with Apache License 2.0 | 6 votes |
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 #8
Source File: test_issue_569.py From python-slackclient with MIT License | 6 votes |
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 |
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 |
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: quotas.py From rekall with GNU General Public License v2.0 | 6 votes |
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 #12
Source File: utils.py From mars with Apache License 2.0 | 6 votes |
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 #13
Source File: cluster.py From pyquarkchain with MIT License | 6 votes |
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 #14
Source File: haproxy_synapse_reaper.py From synapse-tools with Apache License 2.0 | 6 votes |
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 #15
Source File: haproxy_synapse_reaper.py From synapse-tools with Apache License 2.0 | 6 votes |
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 #16
Source File: __init__.py From ACE with Apache License 2.0 | 6 votes |
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 #17
Source File: __init__.py From ACE with Apache License 2.0 | 6 votes |
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 #18
Source File: ctprobe_container_crawler.py From agentless-system-crawler with Apache License 2.0 | 6 votes |
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 |
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: processes.py From pkmeter with BSD 3-Clause "New" or "Revised" License | 5 votes |
def update(self): pids = set() for pid in list(psutil.pids()): try: proc = self.procs.get(pid, {}).get('proc') if not proc: proc = psutil.Process(pid) self.procs[pid] = { 'proc': proc, 'pid': pid, 'cmdline': proc.cmdline(), 'create_time': proc.create_time(), 'name': proc.name(), 'username': proc.username(), } self.procs[pid]['cpu_percent'] = proc.cpu_percent() self.procs[pid]['memory_rss'] = proc.memory_info().rss self.procs[pid]['status'] = proc.status() pids.add(pid) except (psutil.NoSuchProcess, psutil.AccessDenied): pass for pid in [p for p in self.procs if p not in pids]: del self.procs[pid] self.data['sort'] = self.sortkey self.data['total'] = len(self.procs) self.data['procs'] = sorted(self.procs.values(), key=lambda p: p[self.sortkey], reverse=True) super(Plugin, self).update()
Example #21
Source File: webui.py From MPContribs with MIT License | 5 votes |
def stop_processes(): global processes for process_name in processes.keys(): if processes[process_name]: if process_name != "MongodProcess": processes[process_name].terminate() time.sleep(1) processes[process_name] = None parent = psutil.Process(os.getpid()) for child in parent.children(recursive=True): if child.name() == "mongod": child.kill() print("killed mongod")
Example #22
Source File: labview.py From python_labview_automation with MIT License | 5 votes |
def _get_process(self, pid, executable): """ This function attempts to retrieve the psutil.Process for the launched version of LabVIEW. There is a possibility that the previously launched process has closed and a new process with the same pid has been launched, this will do a basic check for this by making sure the pid is the same LabVIEW exe, there is a very remote chance that LabVIEW has relaunched with the same pid, but this seems to be so remote and usually irrelevant to our use-cases anyway that I'm allowing it here. Returns: psutil.Process if exists, None otherwise """ if pid is None: return None try: proc = psutil.Process(pid) if proc.exe().lower() == executable.lower(): return proc return None except psutil.NoSuchProcess: return None
Example #23
Source File: labview.py From python_labview_automation with MIT License | 5 votes |
def kill_process(self, pid, executable, timeout=None): proc = self._get_process(pid, executable) if proc is None: return # Process not running, just exit. proc.kill() proc.wait(timeout)
Example #24
Source File: utils.py From python-pool-performance with MIT License | 5 votes |
def memory_percent(): current_process = psutil.Process(os.getpid()) return current_process.memory_percent() + sum( map( psutil.Process.memory_percent, current_process.children(recursive=True) ) )
Example #25
Source File: system_status.py From Paradrop with Apache License 2.0 | 5 votes |
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 #26
Source File: unicorn_binance_websocket_api_manager.py From unicorn-binance-websocket-api with MIT License | 5 votes |
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 #27
Source File: metrics.py From sanic-prometheus with MIT License | 5 votes |
def periodic_memcollect_task(app, period_sec, loop): p = psutil.Process() while True: await asyncio.sleep(period_sec, loop=loop) app.metrics['PROC_RSS_MEM_BYTES'].set(p.memory_info().rss) app.metrics['PROC_RSS_MEM_PERC'].set(p.memory_percent())
Example #28
Source File: profiler.py From pyGSTi with Apache License 2.0 | 5 votes |
def _get_mem_usage(): p = _psutil.Process(_os.getpid()) return p.memory_info()[0]
Example #29
Source File: rpc.py From brownie with MIT License | 5 votes |
def _at_exit(self) -> None: if not self.is_active(): return if self._rpc.parent() == psutil.Process(): if getattr(self._rpc, "stdout", None) is not None: self._rpc.stdout.close() if getattr(self._rpc, "stderr", None) is not None: self._rpc.stderr.close() self.kill(False)
Example #30
Source File: rpc.py From brownie with MIT License | 5 votes |
def attach(self, laddr: Union[str, Tuple]) -> None: """Attaches to an already running RPC client subprocess. Args: laddr: Address that the client is listening at. Can be supplied as a string "http://127.0.0.1:8545" or tuple ("127.0.0.1", 8545)""" if self.is_active(): raise SystemError("RPC is already active.") if isinstance(laddr, str): o = urlparse(laddr) if not o.port: raise ValueError("No RPC port given") laddr = (o.hostname, o.port) try: proc = next(i for i in psutil.process_iter() if _check_connections(i, laddr)) except StopIteration: raise ProcessLookupError( "Could not attach to RPC process. If this issue persists, try killing " "the RPC and let Brownie launch it as a child process." ) from None print(f"Attached to local RPC client listening at '{laddr[0]}:{laddr[1]}'...") self._rpc = psutil.Process(proc.pid) self._time_offset = self._request("evm_increaseTime", [0]) if web3.provider: self._reset_id = self._current_id = self._snap() _notify_registry(0)