Skip to content

Commit f10af51

Browse files
authored
Merge pull request #248 from mmcneill/stop_repeat_runs
Added max_iterations parameter to memory_usage function
2 parents ad64e49 + 0266baa commit f10af51

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

memory_profiler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def run(self):
238238

239239
def 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):

test/test_memory_usage.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from memory_profiler import memory_usage
2+
import os
23

34

45
def some_func(*args, **kwargs):
@@ -10,6 +11,20 @@ def test_memory_usage():
1011
mem, ret = memory_usage((some_func, (1, 2), dict(a=1)), retval=True)
1112
assert ret[0] == (1, 2)
1213
assert ret[1] == dict(a=1)
14+
15+
16+
def write_line(filepath):
17+
with open(filepath, 'a') as the_file:
18+
the_file.write('Testing\n')
19+
20+
def test_max_iterations():
21+
# Check that memory_usage works with max_iterations set (for python functions).
22+
this_dir = os.path.dirname(os.path.realpath(__file__))
23+
file = os.path.join(this_dir, 'temp_test_max_iterations_file.txt')
24+
mem = memory_usage((write_line, (file, ), dict()), max_usage=True, max_iterations=1)
25+
n_lines = sum(1 for line in open(file))
26+
os.remove(file)
27+
assert n_lines == 1
1328

1429

1530
def test_return_value_consistency():
@@ -27,4 +42,5 @@ def test_return_value_consistency():
2742

2843
if __name__ == "__main__":
2944
test_memory_usage()
45+
test_max_iterations()
3046
test_return_value_consistency()

0 commit comments

Comments
 (0)