Permalink
Browse files

memory_usage working and some documentation.

  • Loading branch information...
1 parent e07028e commit c897818d18c2b68278c6df2618ddc142eaa95a1c @fabianp committed Aug 23, 2012
Showing with 53 additions and 22 deletions.
  1. +28 −0 README.rst
  2. +25 −22 memory_profiler.py
View
@@ -69,6 +69,34 @@ with respect to the last one. The last column (*Line Contents*) prints
the code that has been profiled.
+====
+API
+====
+
+memory_profiler exposes a number of functions to be used in third-party
+code.
+
+`memory_usage(proc=-1, num=-1, interval=.1)` returns the memory usage
+over a time interval. The first argument, `proc` represents what
+should be monitored. This can either be the PID of a process (not
+necessarily a Python program), a string containing some python code to
+be evaluated or a tuple (f, args, kw) containing a function and its
+arguments to be evaluated as f(*args, **kw). For example::
+
+
+ >>> # define a simple function
+ >>> def f(a, n=100):
+ ... import time
+ ... time.sleep(2)
+ ... b = [a] * n
+ ... time.sleep(1)
+ ... return b
+ ...
+ >>> from memory_profiler import memory_usage
+ >>> memory_usage((f, (1,), {'n' : int(1e6)}))
+
+
+
===================
Ipython integration
View
@@ -5,7 +5,7 @@
_CMD_USAGE = "python -m memory_profiler script_file.py"
import time, sys, os, pdb
-import warnings
+import warnings, multiprocessing
import linecache
import inspect
@@ -41,18 +41,17 @@ def _get_memory(pid):
'platforms')
-
def memory_usage(proc=-1, num=-1, interval=.1):
"""
Return the memory usage of a process or piece of code
Parameters
----------
- proc : {int, string, tuple}
- The process to monitor. Can be given by a PID or by a string
- containing a filename. A tuple containing (f, args, kwargs) specifies
- to run the function f(*args, **kwargs). Set to -1 (default)for
- current process.
+ proc : {int, string, tuple}, optional
+ The process to monitor. Can be given by a PID, by a string
+ containing a filename or by a tuple. The tuple should contain
+ three values (f, args, kw) specifies to run the function
+ f(*args, **kw). Set to -1 (default) for current process.
interval : int, optional
@@ -61,8 +60,6 @@ def memory_usage(proc=-1, num=-1, interval=.1):
to wait until the process has finished if proc is a string or
to get just one if proc is an integer.
- locals : dict
- Local variables.
Returns
-------
@@ -71,29 +68,35 @@ def memory_usage(proc=-1, num=-1, interval=.1):
"""
ret = []
+
if str(proc).endswith('.py'):
filename = _find_script(proc)
with open(filename) as f:
proc = f.read()
-
- # TODO: make sure script's directory is on sys.path
- def f_exec(x, locals):
- # function interface for exec
- exec(x, locals)
- proc = (f_exec, (), {})
+ raise NotImplementedError
if isinstance(proc, (list, tuple)):
- from multiprocessing import Process
+
if len(proc) == 1:
- proc = (proc[0], (), {})
+ f, args, kw = (proc[0], (), {})
elif len(proc) == 2:
- proc = (proc[0], proc[1], {})
- p = Process(target=proc[0], args=proc[1], kwargs=proc[2])
- p.start()
- while p.is_alive(): # FIXME: or num
- ret.append(_get_memory(p.pid))
+ f, args, kw = (proc[0], proc[1], {})
+ elif len(proc) == 3:
+ f, args, kw = (proc[0], proc[1], proc[2])
+ else:
+ raise ValueError
+ main_thread = multiprocessing.Process(target=f, args=args, kwargs=kw)
+ i = 0
+ max_iter = num if num > 0 else float("inf")
+ main_thread.start()
+ while i < max_iter and main_thread.is_alive():
+ m = _get_memory(main_thread.pid)
+ ret.append(m)
time.sleep(interval)
+ i += 1
+ main_thread.join()
else:
+ # external process
if proc == -1:
proc = os.getpid()
if num == -1:

0 comments on commit c897818

Please sign in to comment.