@@ -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
@@ -307,6 +307,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
307307 else :
308308 # for a Python function wait until it finishes
309309 max_iter = float ('inf' )
310+ if max_iterations is not None :
311+ max_iter = max_iterations
310312
311313 if callable (proc ):
312314 proc = (proc , (), {})
@@ -320,7 +322,9 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
320322 else :
321323 raise ValueError
322324
325+ current_iter = 0
323326 while True :
327+ current_iter += 1
324328 child_conn , parent_conn = Pipe () # this will store MemTimer's results
325329 p = MemTimer (os .getpid (), interval , child_conn , backend ,
326330 timestamps = timestamps ,
@@ -349,7 +353,8 @@ def memory_usage(proc=-1, interval=.1, timeout=None, timestamps=False,
349353 raise
350354
351355 p .join (5 * interval )
352- if n_measurements > 4 or interval < 1e-6 :
356+
357+ if (n_measurements > 4 ) or (current_iter == max_iter ) or (interval < 1e-6 ):
353358 break
354359 interval /= 10.
355360 elif isinstance (proc , subprocess .Popen ):
0 commit comments