33# .. we'll use this to pass it to the child script ..
44_CLEAN_GLOBALS = globals ().copy ()
55
6- __version__ = '0.47 '
6+ __version__ = '0.48.dev0 '
77
88_CMD_USAGE = "python -m memory_profiler script_file.py"
99
1919import traceback
2020from signal import SIGKILL
2121
22+ import psutil
23+
2224
2325# TODO: provide alternative when multiprocessing is not available
2426try :
@@ -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-
5853try :
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 ))
0 commit comments