首页 > 解决方案 > 使用一个函数调用另一个函数

问题描述

我正在尝试使用该memory_profiler软件包。下面的示例代码:

from memory_profiler import memory_usage
from time import sleep

def f():
    # a function that with growing
    # memory consumption
    a = [0] * 1000
    sleep(.1)
    b = a * 100
    sleep(.1)
    c = b * 100
    return a

mem_usage = memory_usage(f)
print('Memory usage (in chunks of .1 seconds): %s' % mem_usage)
print('Maximum memory usage: %s' % max(mem_usage))

我面临的问题是我想尝试的实际功能需要参数。

def my_func(df1, df2):
    new_df = pd.concat([df1, df2])
    return new_df

有没有办法创建一个运行我的真实函数的shell函数,以便我可以使用这个包?

def shell_function():
    new_df = my_func(df1, df1)
    return new_df

代码来自这篇文章的第二个答案:Tracking *maximum* memory usage by a Python function

标签: pythonmemory-profiling

解决方案


推荐阅读