Returns a generator that yields memory for all child processes.
(process, meminfo_attr=None, memory_metric=0)
| 85 | |
| 86 | |
| 87 | def _get_child_memory(process, meminfo_attr=None, memory_metric=0): |
| 88 | """ |
| 89 | Returns a generator that yields memory for all child processes. |
| 90 | """ |
| 91 | # Convert a pid to a process |
| 92 | if isinstance(process, int): |
| 93 | if process == -1: process = os.getpid() |
| 94 | process = psutil.Process(process) |
| 95 | |
| 96 | if not meminfo_attr: |
| 97 | # Use the psutil 2.0 attr if the older version isn't passed in. |
| 98 | meminfo_attr = 'memory_info' if hasattr(process, 'memory_info') else 'get_memory_info' |
| 99 | |
| 100 | # Select the psutil function get the children similar to how we selected |
| 101 | # the memory_info attr (a change from excepting the AttributeError). |
| 102 | children_attr = 'children' if hasattr(process, 'children') else 'get_children' |
| 103 | |
| 104 | # Loop over the child processes and yield their memory |
| 105 | try: |
| 106 | for child in getattr(process, children_attr)(recursive=True): |
| 107 | if isinstance(memory_metric, str): |
| 108 | meminfo = getattr(child, meminfo_attr)() |
| 109 | yield child.pid, getattr(meminfo, memory_metric) / _TWO_20 |
| 110 | else: |
| 111 | yield child.pid, getattr(child, meminfo_attr)()[memory_metric] / _TWO_20 |
| 112 | except (psutil.NoSuchProcess, psutil.AccessDenied): |
| 113 | # https://github.com/fabianp/memory_profiler/issues/71 |
| 114 | yield (0, 0.0) |
| 115 | |
| 116 | |
| 117 | def _get_memory(pid, backend, timestamps=False, include_children=False, filename=None): |
no outgoing calls
no test coverage detected