@@ -238,7 +238,7 @@ def run(self):
238238
239239def memory_usage (proc = - 1 , interval = .1 , timeout = None , timestamps = False ,
240240 include_children = False , multiprocess = False , max_usage = False ,
241- retval = False , stream = None , backend = None ):
241+ retval = False , stream = None , backend = None , max_iterations = None ):
242242 """
243243 Return the memory usage of a process or piece of code
244244
@@ -280,6 +280,10 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
280280 to this file instead of stored in memory and returned at the end of
281281 the subprocess. Useful for long-running processes.
282282 Implies timestamps=True.
283+
284+ max_iterations : int
285+ Limits the number of iterations (calls to the process being monitored). Relevent
286+ when the process is a python function.
283287
284288 Returns
285289 -------
@@ -307,6 +311,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
307311 else :
308312 # for a Python function wait until it finishes
309313 max_iter = float ('inf' )
314+ if max_iterations is not None :
315+ max_iter = max_iterations
310316
311317 if callable (proc ):
312318 proc = (proc , (), {})
@@ -320,7 +326,9 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
320326 else :
321327 raise ValueError
322328
329+ current_iter = 0
323330 while True :
331+ current_iter += 1
324332 child_conn , parent_conn = Pipe () # this will store MemTimer's results
325333 p = MemTimer (os .getpid (), interval , child_conn , backend ,
326334 timestamps = timestamps ,
@@ -349,7 +357,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
349357 raise
350358
351359 p .join (5 * interval )
352- if n_measurements > 4 or interval < 1e-6 :
360+
361+ if (n_measurements > 4 ) or (current_iter == max_iter ) or (interval < 1e-6 ):
353362 break
354363 interval /= 10.
355364 elif isinstance (proc , subprocess .Popen ):
0 commit comments