Skip to content

Commit a3c4b58

Browse files
committed
WIP: Make psutil required
Now the import does not work, needs fixing.
1 parent e2a97b9 commit a3c4b58

3 files changed

Lines changed: 10 additions & 27 deletions

File tree

README.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
This is a python module for monitoring memory consumption of a process
99
as well as line-by-line analysis of memory consumption for python
10-
programs. It is a pure python module and has the `psutil
11-
<http://pypi.python.org/pypi/psutil>`_ module as optional (but highly
12-
recommended) dependencies.
10+
programs. It is a pure python module which depends on the `psutil
11+
<http://pypi.python.org/pypi/psutil>`_ module.
1312

1413

1514
==============
@@ -385,7 +384,7 @@ file ~/.ipython/ipy_user_conf.py to add the following lines::
385384
between runs.
386385

387386
* Q: Does it work under windows ?
388-
* A: Yes, but you will need the
387+
* A: Yes, thanks to the
389388
`psutil <http://pypi.python.org/pypi/psutil>`_ module.
390389

391390

memory_profiler.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import traceback
2020
from signal import SIGKILL
2121

22+
import psutil
23+
2224

2325
# TODO: provide alternative when multiprocessing is not available
2426
try:
@@ -48,13 +50,6 @@ def unicode(x, *args):
4850
return str(x)
4951

5052
# .. get available packages ..
51-
try:
52-
import psutil
53-
54-
has_psutil = True
55-
except ImportError:
56-
has_psutil = False
57-
5853
try:
5954
import tracemalloc
6055

@@ -92,12 +87,6 @@ def _get_child_memory(process, meminfo_attr=None):
9287
"""
9388
Returns a generator that yields memory for all child processes.
9489
"""
95-
if not has_psutil:
96-
raise NotImplementedError((
97-
"The psutil module is required to monitor the "
98-
"memory usage of child processes."
99-
))
100-
10190
# Convert a pid to a process
10291
if isinstance(process, int):
10392
if process == -1: process = os.getpid()
@@ -344,10 +333,9 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
344333
if retval:
345334
ret = ret, returned
346335
except Exception:
347-
if has_psutil:
348-
parent = psutil.Process(os.getpid())
349-
for child in parent.children(recursive=True):
350-
os.kill(child.pid, SIGKILL)
336+
parent = psutil.Process(os.getpid())
337+
for child in parent.children(recursive=True):
338+
os.kill(child.pid, SIGKILL)
351339
p.join(0)
352340
raise
353341

@@ -1086,10 +1074,9 @@ def choose_backend(new_backend=None):
10861074

10871075
_backend = 'no_backend'
10881076
all_backends = [
1089-
('psutil', has_psutil),
1077+
('psutil', True),
10901078
('posix', os.name == 'posix'),
10911079
('tracemalloc', has_tracemalloc),
1092-
('no_backend', True)
10931080
]
10941081
backends_indices = dict((b[0], i) for i, b in enumerate(all_backends))
10951082

@@ -1100,10 +1087,6 @@ def choose_backend(new_backend=None):
11001087
if is_available:
11011088
_backend = n_backend
11021089
break
1103-
if _backend == 'no_backend':
1104-
raise NotImplementedError(
1105-
'Tracemalloc or psutil module is required for non-unix '
1106-
'platforms')
11071090
if _backend != new_backend and new_backend is not None:
11081091
warnings.warn('{0} can not be used, {1} used instead'.format(
11091092
new_backend, _backend))

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
url='http://pypi.python.org/pypi/memory_profiler',
3131
py_modules=['memory_profiler'],
3232
scripts=['mprof'],
33+
install_requires=['psutil'],
3334
classifiers=[_f for _f in CLASSIFIERS.split('\n') if _f],
3435
license='BSD'
3536

0 commit comments

Comments
 (0)