Python psutil.Error() Examples
The following are 30
code examples of psutil.Error().
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_process.py From vnpy_crypto with MIT License | 6 votes |
def test_children_duplicates(self): # find the process which has the highest number of children table = collections.defaultdict(int) for p in psutil.process_iter(): try: table[p.ppid()] += 1 except psutil.Error: pass # this is the one, now let's make sure there are no duplicates pid = sorted(table.items(), key=lambda x: x[1])[-1][0] p = psutil.Process(pid) try: c = p.children(recursive=True) except psutil.AccessDenied: # windows pass else: self.assertEqual(len(c), len(set(c)))
Example #2
Source File: test_process.py From jarvis with GNU General Public License v2.0 | 6 votes |
def test_children_duplicates(self): # find the process which has the highest number of children table = collections.defaultdict(int) for p in psutil.process_iter(): try: table[p.ppid()] += 1 except psutil.Error: pass # this is the one, now let's make sure there are no duplicates pid = sorted(table.items(), key=lambda x: x[1])[-1][0] p = psutil.Process(pid) try: c = p.children(recursive=True) except psutil.AccessDenied: # windows pass else: self.assertEqual(len(c), len(set(c)))
Example #3
Source File: test_postmaster.py From patroni with MIT License | 6 votes |
def test_signal_kill(self, mock_kill, mock_children, mock_suspend): proc = PostmasterProcess(123) # all processes successfully stopped mock_children.return_value = [Mock()] mock_children.return_value[0].kill.side_effect = psutil.Error self.assertTrue(proc.signal_kill()) # postmaster has gone before suspend mock_suspend.side_effect = psutil.NoSuchProcess(123) self.assertTrue(proc.signal_kill()) # postmaster has gone before we got a list of children mock_suspend.side_effect = psutil.Error() mock_children.side_effect = psutil.NoSuchProcess(123) self.assertTrue(proc.signal_kill()) # postmaster has gone after we got a list of children mock_children.side_effect = psutil.Error() mock_kill.side_effect = psutil.NoSuchProcess(123) self.assertTrue(proc.signal_kill()) # failed to kill postmaster mock_kill.side_effect = psutil.AccessDenied(123) self.assertFalse(proc.signal_kill())
Example #4
Source File: test_process.py From psutil with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_children_duplicates(self): # find the process which has the highest number of children table = collections.defaultdict(int) for p in psutil.process_iter(): try: table[p.ppid()] += 1 except psutil.Error: pass # this is the one, now let's make sure there are no duplicates pid = sorted(table.items(), key=lambda x: x[1])[-1][0] if LINUX and pid == 0: raise self.skipTest("PID 0") p = psutil.Process(pid) try: c = p.children(recursive=True) except psutil.AccessDenied: # windows pass else: self.assertEqual(len(c), len(set(c)))
Example #5
Source File: test_callback_executor.py From patroni with MIT License | 6 votes |
def test_callback_executor(self, mock_popen): mock_popen.return_value.children.return_value = [] mock_popen.return_value.is_running.return_value = True ce = CallbackExecutor() ce._kill_children = Mock(side_effect=Exception) self.assertIsNone(ce.call([])) ce.join() self.assertIsNone(ce.call([])) mock_popen.return_value.kill.side_effect = psutil.AccessDenied() self.assertIsNone(ce.call([])) ce._process_children = [] mock_popen.return_value.children.side_effect = psutil.Error() mock_popen.return_value.kill.side_effect = psutil.NoSuchProcess(123) self.assertIsNone(ce.call([])) mock_popen.side_effect = Exception ce = CallbackExecutor() ce._condition.wait = Mock(side_effect=[None, Exception]) self.assertIsNone(ce.call([])) ce.join()
Example #6
Source File: cancellable.py From patroni with MIT License | 6 votes |
def _kill_process(self): with self._lock: if self._process is not None and self._process.is_running() and not self._process_children: try: self._process.suspend() # Suspend the process before getting list of childrens except psutil.Error as e: logger.info('Failed to suspend the process: %s', e.msg) try: self._process_children = self._process.children(recursive=True) except psutil.Error: pass try: self._process.kill() logger.warning('Killed %s because it was still running', self._process_cmd) except psutil.NoSuchProcess: pass except psutil.AccessDenied as e: logger.warning('Failed to kill the process: %s', e.msg)
Example #7
Source File: xprocess.py From pytest-xprocess with MIT License | 6 votes |
def terminate(self): # return codes: # 0 no work to do # 1 terminated # -1 failed to terminate if not self.pid or not self.isrunning(): return 0 timeout = 20 try: proc = psutil.Process(self.pid) proc.terminate() try: proc.wait(timeout=timeout/2) except psutil.TimeoutExpired: proc.kill() proc.wait(timeout=timeout/2) except psutil.Error: return -1 return 1
Example #8
Source File: task.py From trains with Apache License 2.0 | 6 votes |
def _kill_all_child_processes(send_kill=False): # get current process if pid not provided pid = os.getpid() try: parent = psutil.Process(pid) except psutil.Error: # could not find parent process id return for child in parent.children(recursive=True): if send_kill: child.kill() else: child.terminate() # kill ourselves if send_kill: parent.kill() else: parent.terminate()
Example #9
Source File: scripts.py From ray with Apache License 2.0 | 6 votes |
def up(cluster_config_file, min_workers, max_workers, no_restart, restart_only, yes, cluster_name): """Create or update a Ray cluster.""" if restart_only or no_restart: assert restart_only != no_restart, "Cannot set both 'restart_only' " \ "and 'no_restart' at the same time!" if urllib.parse.urlparse(cluster_config_file).scheme in ("http", "https"): try: response = urllib.request.urlopen(cluster_config_file, timeout=5) content = response.read() file_name = cluster_config_file.split("/")[-1] with open(file_name, "wb") as f: f.write(content) cluster_config_file = file_name except urllib.error.HTTPError as e: logger.info("Error downloading file: ", e) create_or_update_cluster(cluster_config_file, min_workers, max_workers, no_restart, restart_only, yes, cluster_name)
Example #10
Source File: process.py From trains-agent with Apache License 2.0 | 6 votes |
def kill_all_child_processes(pid=None): # get current process if pid not provided include_parent = True if not pid: pid = os.getpid() include_parent = False print("\nLeaving process id {}".format(pid)) try: parent = psutil.Process(pid) except psutil.Error: # could not find parent process id return for child in parent.children(recursive=True): child.kill() if include_parent: parent.kill()
Example #11
Source File: __init__.py From pyTSon_plugins with GNU General Public License v3.0 | 5 votes |
def commandB64Decode(self, schid, targetMode, toID, fromID, params=""): try: msg = b64decode(params.encode('utf-8')).decode('utf-8') except Exception as ex: msg = "Error while decoding: {}".format(ex.__class__.__name__) self.answerMessage(schid, targetMode, toID, fromID, msg)
Example #12
Source File: posix.py From Pokemon-Terminal with GNU General Public License v3.0 | 5 votes |
def __has_open_file_handles_real(path: PosixPath) -> bool: for proc in psutil.process_iter(): try: if proc.pid != os.getpid(): for file in proc.open_files(): if PosixPath(file.path).samefile(path): return True except psutil.Error: continue return False
Example #13
Source File: test_examples.py From tchannel-python with MIT License | 5 votes |
def popen(path, wait_for_listen=False): process = psutil.Popen( ['python', path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) if wait_for_listen: # It would be more correct to check ``conn.status == # psutil.CONN_LISTEN`` but this works try: while process.is_running() and not process.connections(): pass except psutil.Error: raise AssertionError(process.stderr.read()) success = True # noinspection PyBroadException try: yield process except: success = False raise finally: process.kill() if not success: dump_std_streams(path, process)
Example #14
Source File: test_windows.py From jarvis with GNU General Public License v2.0 | 5 votes |
def test_exe(self): for p in psutil.process_iter(): try: self.assertEqual(os.path.basename(p.exe()), p.name()) except psutil.Error: pass
Example #15
Source File: postmaster.py From patroni with MIT License | 5 votes |
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 #16
Source File: postmaster.py From patroni with MIT License | 5 votes |
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 #17
Source File: processes.py From rekall with GNU General Public License v2.0 | 5 votes |
def _get_field(self, field_name): try: result = getattr(self._proc, field_name) if callable(result): result = result() return result except psutil.Error: # Some processes do not have environ defined. if field_name == "environ": return {} return None except AttributeError: return None
Example #18
Source File: __init__.py From pyTSon_plugins with GNU General Public License v3.0 | 5 votes |
def commandB64Encode(self, schid, targetMode, toID, fromID, params=""): try: msg = b64encode(params.encode('utf-8')).decode('utf-8') except Exception as ex: msg = "Error while encoding: {}".format(ex.__class__.__name__) self.answerMessage(schid, targetMode, toID, fromID, msg)
Example #19
Source File: __init__.py From pyTSon_plugins with GNU General Public License v3.0 | 5 votes |
def commandTaskList(self, schid, targetMode, toID, fromID, params=""): import psutil msg = [] for p in psutil.process_iter(): try: _p = str(p.as_dict(attrs=['name'])['name']) if ".exe" in _p.lower(): msg.extend([_p]) except psutil.Error: pass msg = '\n'.join(sorted(msg)) self.answerMessage(schid, targetMode, toID, fromID, msg)
Example #20
Source File: runtime_info.py From openrasp-iast with Apache License 2.0 | 5 votes |
def refresh_info(self): """ 获取当前所有模块的运行状态,并存入历史记录 """ self._refresh_system_info() shared_info = Communicator().dump_shared_mem() for module_name in shared_info: try: pid = shared_info[module_name]["pid"] if pid == 0: continue module_procs = self._get_psutil_proc(pid, module_name) psutil_proc = module_procs["proc"] sub_procs = module_procs["subprocs"] cpu = psutil_proc.cpu_percent(interval=None) mem = psutil_proc.memory_info().rss for subproc in sub_procs: try: cpu += subproc.cpu_percent(interval=None) mem += subproc.memory_info().rss except Exception: pass shared_info[module_name]["cpu"] = str(cpu) + "%" shared_info[module_name]["mem"] = common.bytes2human(mem) except psutil.Error as e: Logger().warning("Detect module {} may exited!".format(module_name)) if module_name != "Preprocessor": Communicator().set_value("pid", 0, module_name) shared_info[module_name]["cpu"] = -1 shared_info[module_name]["mem"] = -1 with self.lock: self.history_info.pop(0) self.history_info.append(shared_info)
Example #21
Source File: pstree.py From psutil with BSD 3-Clause "New" or "Revised" License | 5 votes |
def print_tree(parent, tree, indent=''): try: name = psutil.Process(parent).name() except psutil.Error: name = "?" print(parent, name) if parent not in tree: return children = tree[parent][:-1] for child in children: sys.stdout.write(indent + "|- ") print_tree(child, tree, indent + "| ") child = tree[parent][-1] sys.stdout.write(indent + "`_ ") print_tree(child, tree, indent + " ")
Example #22
Source File: test_contracts.py From psutil with BSD 3-Clause "New" or "Revised" License | 5 votes |
def proc_info(pid): tcase = PsutilTestCase() def check_exception(exc, proc, name, ppid): tcase.assertEqual(exc.pid, pid) tcase.assertEqual(exc.name, name) if isinstance(exc, psutil.ZombieProcess): if exc.ppid is not None: tcase.assertGreaterEqual(exc.ppid, 0) tcase.assertEqual(exc.ppid, ppid) elif isinstance(exc, psutil.NoSuchProcess): tcase.assertProcessGone(proc) str(exc) assert exc.msg def do_wait(): if pid != 0: try: proc.wait(0) except psutil.Error as exc: check_exception(exc, proc, name, ppid) try: proc = psutil.Process(pid) d = proc.as_dict(['ppid', 'name']) except psutil.NoSuchProcess: return {} name, ppid = d['name'], d['ppid'] info = {'pid': proc.pid} ns = process_namespace(proc) with proc.oneshot(): for fun, fun_name in ns.iter(ns.getters, clear_cache=False): try: info[fun_name] = fun() except psutil.Error as exc: check_exception(exc, proc, name, ppid) continue do_wait() return info
Example #23
Source File: monaco_stats.py From monaco with MIT License | 5 votes |
def run(self): ''' Runs until no longer valid (app moved, not master, etc), or repeated errors over self.threshold ''' while self._run: start_time = time.time() if self.failcount >= self.threshold: self.logger.error('Node stat reporter thread for Monaco DB is sleeping to prevent thrashing') time.sleep(15) # Collect/Publish stats try: info = self.r.info() for stat in ['connected_slaves', 'used_memory', 'connected_clients', 'instantaneous_ops_per_sec']: PUBLISHER.publish_monaco_stat(self.node_id, stat, info[stat]) proc = self.redmgr.instance_proc('6379') if proc: PUBLISHER.publish_monaco_stat(self.node_id, 'cpu_percent', proc.cpu_percent(interval=self.interval)) except redis.ConnectionError: self.logger.info('Redis error accessing Monaco DB info') del self.r self.r = redis.StrictRedis(port=config.config['mgmt_port']) except psutil.Error, err: self.logger.info('psutil error - %s', repr(err)) self.failcount += 1 except Exception, exc: self.logger.exception(exc) self.failcount += 1
Example #24
Source File: test_windows.py From vnpy_crypto with MIT License | 5 votes |
def test_exe(self): for p in psutil.process_iter(): try: self.assertEqual(os.path.basename(p.exe()), p.name()) except psutil.Error: pass
Example #25
Source File: app.py From BeautifulDiscord with MIT License | 5 votes |
def discord_process(): executables = {} for proc in psutil.process_iter(): try: (path, exe) = os.path.split(proc.exe()) except (psutil.Error, OSError): pass else: if exe.startswith('Discord') and not exe.endswith('Helper'): entry = executables.get(exe) if entry is None: entry = executables[exe] = DiscordProcess(path=path, exe=exe) entry.processes.append(proc) if len(executables) == 0: raise RuntimeError('Could not find Discord executable.') if len(executables) == 1: r = executables.popitem() print('Found {0.exe} under {0.path}'.format(r[1])) return r[1] lookup = list(executables) for index, exe in enumerate(lookup): print('%s: Found %s' % (index, exe)) while True: index = input("Discord executable to use (number): ") try: index = int(index) except ValueError as e: print('Invalid index passed') else: if index >= len(lookup) or index < 0: print('Index too big (or small)') else: key = lookup[index] return executables[key]
Example #26
Source File: __init__.py From platypush with MIT License | 4 votes |
def processes(self, filter: Optional[str] = '') -> ProcessResponseList: """ Get the list of running processes. :param filter: Filter the list by name. :return: List of :class:`platypush.message.response.system.ProcessResponse`. """ import psutil processes = [psutil.Process(pid) for pid in psutil.pids()] p_list = [] for p in processes: if filter and filter not in p.name(): continue args = {} try: mem = p.memory_info() times = p.cpu_times() args.update( pid=p.pid, name=p.name(), started=datetime.fromtimestamp(p.create_time()), ppid=p.ppid(), children=[pp.pid for pp in p.children()], status=p.status(), username=p.username(), terminal=p.terminal(), cpu_user_time=times.user, cpu_system_time=times.system, cpu_children_user_time=times.children_user, cpu_children_system_time=times.children_system, mem_rss=mem.rss, mem_vms=mem.vms, mem_shared=mem.shared, mem_text=mem.text, mem_data=mem.data, mem_lib=mem.lib, mem_dirty=mem.dirty, mem_percent=p.memory_percent(), ) except psutil.Error: continue try: args.update( exe=p.exe(), ) except psutil.Error: pass p_list.append(ProcessResponse(**args)) return ProcessResponseList(p_list)
Example #27
Source File: core.py From daemonocle with MIT License | 4 votes |
def status(self): """Get the status of the daemon.""" if self.pidfile is None: raise DaemonError('Cannot get status of daemon without PID file') pid = self._read_pidfile() if pid is None: self._emit_message( '{prog} -- not running\n'.format(prog=self.prog)) sys.exit(1) proc = psutil.Process(pid) # Default data data = { 'prog': self.prog, 'pid': pid, 'status': proc.status(), 'uptime': '0m', 'cpu': 0.0, 'memory': 0.0, } # Add up all the CPU and memory usage of all the # processes in the process group pgid = os.getpgid(pid) for gproc in psutil.process_iter(): try: if os.getpgid(gproc.pid) == pgid and gproc.pid != 0: data['cpu'] += gproc.cpu_percent(interval=0.1) data['memory'] += gproc.memory_percent() except (psutil.Error, OSError): continue # Calculate the uptime and format it in a human-readable but # also machine-parsable format try: uptime_mins = int(round((time.time() - proc.create_time()) / 60)) uptime_hours, uptime_mins = divmod(uptime_mins, 60) data['uptime'] = str(uptime_mins) + 'm' if uptime_hours: uptime_days, uptime_hours = divmod(uptime_hours, 24) data['uptime'] = str(uptime_hours) + 'h ' + data['uptime'] if uptime_days: data['uptime'] = str(uptime_days) + 'd ' + data['uptime'] except psutil.Error: pass template = ('{prog} -- pid: {pid}, status: {status}, ' 'uptime: {uptime}, %cpu: {cpu:.1f}, %mem: {memory:.1f}\n') self._emit_message(template.format(**data))
Example #28
Source File: ps.py From psutil with BSD 3-Clause "New" or "Revised" License | 4 votes |
def main(): today_day = datetime.date.today() templ = "%-10s %5s %5s %7s %7s %5s %6s %6s %6s %s" attrs = ['pid', 'memory_percent', 'name', 'cmdline', 'cpu_times', 'create_time', 'memory_info', 'status', 'nice', 'username'] print(templ % ("USER", "PID", "%MEM", "VSZ", "RSS", "NICE", "STATUS", "START", "TIME", "CMDLINE")) for p in psutil.process_iter(attrs, ad_value=None): if p.info['create_time']: ctime = datetime.datetime.fromtimestamp(p.info['create_time']) if ctime.date() == today_day: ctime = ctime.strftime("%H:%M") else: ctime = ctime.strftime("%b%d") else: ctime = '' if p.info['cpu_times']: cputime = time.strftime("%M:%S", time.localtime(sum(p.info['cpu_times']))) else: cputime = '' user = p.info['username'] if not user and psutil.POSIX: try: user = p.uids()[0] except psutil.Error: pass if user and psutil.WINDOWS and '\\' in user: user = user.split('\\')[1] if not user: user = '' user = user[:9] vms = bytes2human(p.info['memory_info'].vms) if \ p.info['memory_info'] is not None else '' rss = bytes2human(p.info['memory_info'].rss) if \ p.info['memory_info'] is not None else '' memp = round(p.info['memory_percent'], 1) if \ p.info['memory_percent'] is not None else '' nice = int(p.info['nice']) if p.info['nice'] else '' if p.info['cmdline']: cmdline = ' '.join(p.info['cmdline']) else: cmdline = p.info['name'] status = p.info['status'][:5] if p.info['status'] else '' line = templ % ( user, p.info['pid'], memp, vms, rss, nice, status, ctime, cputime, cmdline) print(line[:get_terminal_size()[0]])
Example #29
Source File: iotop.py From psutil with BSD 3-Clause "New" or "Revised" License | 4 votes |
def poll(interval): """Calculate IO usage by comparing IO statics before and after the interval. Return a tuple including all currently running processes sorted by IO activity and total disks I/O activity. """ # first get a list of all processes and disk io counters procs = [p for p in psutil.process_iter()] for p in procs[:]: try: p._before = p.io_counters() except psutil.Error: procs.remove(p) continue disks_before = psutil.disk_io_counters() # sleep some time time.sleep(interval) # then retrieve the same info again for p in procs[:]: with p.oneshot(): try: p._after = p.io_counters() p._cmdline = ' '.join(p.cmdline()) if not p._cmdline: p._cmdline = p.name() p._username = p.username() except (psutil.NoSuchProcess, psutil.ZombieProcess): procs.remove(p) disks_after = psutil.disk_io_counters() # finally calculate results by comparing data before and # after the interval for p in procs: p._read_per_sec = p._after.read_bytes - p._before.read_bytes p._write_per_sec = p._after.write_bytes - p._before.write_bytes p._total = p._read_per_sec + p._write_per_sec disks_read_per_sec = disks_after.read_bytes - disks_before.read_bytes disks_write_per_sec = disks_after.write_bytes - disks_before.write_bytes # sort processes by total disk IO so that the more intensive # ones get listed first processes = sorted(procs, key=lambda p: p._total, reverse=True) return (processes, disks_read_per_sec, disks_write_per_sec)
Example #30
Source File: monaco_stats.py From monaco with MIT License | 4 votes |
def run(self): ''' Runs until no longer valid (app moved, not master, etc), or repeated errors over self.threshold ''' self.r = redis.StrictRedis(port=config.config['mgmt_port']) while self._run: start_time = time.time() if self.failcount >= self.threshold: self.logger.warn('Node stat reporter thread for twem %s is terminating from thrashing', self.twem_id) return # Refresh App and verify our responsibility try: self.twem.refresh(self.r) if not self.node_id in self.twem.nodes: # No longer this node's responsibility to report stats return data = self.get_twem_stat() self.logger.debug(repr(data)) except redis.RedisError: self.logger.info('RedisError updating twem %s', self.twem_id) self.r = redis.StrictRedis(port=config.config['mgmt_port']) self.failcount += 1 continue except Exception, exc: self.logger.exception(exc) self.failcount += 1 continue # Collect/Publish stats try: for stat in ['client_connections', 'client_err', 'forward_error']: PUBLISHER.publish_twem_stat(self.twem_id, self.host, stat, data[stat]) proc = self.nutmgr.nutcracker_instance_proc(self.twem) if proc: PUBLISHER.publish_twem_stat(self.twem_id, self.host, 'cpu_percent', proc.cpu_percent(interval=self.interval)) PUBLISHER.publish_twem_stat(self.twem_id, self.host, 'host_unavailable', 0) except psutil.Error, err: self.logger.info('psutil error - %s', repr(err)) time.sleep(self.interval) # since the wait time might not have been observed self.failcount += 1