Python执行器-以参数作为参数传递函数

问题描述

我的目标是创建一个可以用来衡量另一个函数的执行和资源使用情况的函数。通过使用教程,我使用Python的ThreadPoolExecutor创建了以下内容:

from resource import *
from time import sleep
from concurrent.futures import ThreadPoolExecutor

class MemoryMonitor:
    def __init__(self):
        self.keep_measuring = True

    def measure_usage(self):
        max_usage = 0
        u_run_time = 0
        s_run_time = 0
        while self.keep_measuring:
            max_usage = max(max_usage,getrusage(RUSAGE_SELF).ru_maxrss)
            u_run_time = max(u_run_time,getrusage(RUSAGE_SELF).ru_utime) 
            s_run_time = max(s_run_time,getrusage(RUSAGE_SELF).ru_stime) 
        sleep(0.1) # run this loop every 0.1 seconds
        return [max_usage,u_run_time,s_run_time]

def execute(function):
    with ThreadPoolExecutor() as executor:
        monitor = MemoryMonitor()
        stats_thread = executor.submit(monitor.measure_usage)
        try:
            fn_thread = executor.submit(function)
            result = fn_thread.result()
            print("print result")
            print(result)
            print("print result type")
            print(type(result))
        finally:
            monitor.keep_measuring = False
            stats = stats_thread.result()
        print(stats)
        return result

def foo():
    i = 0
    while i < 3:
        print("foo")
        i+=1
    return 1

def bar(x):
    while x < 3:
        print("foobar")
        x+=1
    return 1

var = execute(foo)
print("Var = " + str(var))
var = execute(bar(0))
print("Var = " + str(var))

如果我将函数 foo 作为参数传递给函数 execute ,它将打印正确的结果并返回foo返回的值。

如果我以相同的方式传递函数 bar ,但是bar本身需要一个参数,则该函数运行(打印3次),然后出现以下错误:

result = self.fn(*self.args,**self.kwargs)
TypeError: 'int' object is not callable

经过一些测试,如果该函数本身需要参数,那么我被卡住的部分似乎正在将函数作为参数传递。据我了解ThreadPoolExecutor, fn_thread 对象封装了所提交函数的执行。 result 对象应该只保存执行结果-我想念的是这无法处理带有参数的函数传递吗?

解决方法

您正在提交

bar(0) 

代替

bar,0

为澄清起见,请查看提交者的签名: 提交(fn,* args,** kwargs)

结果

bar(0)

是一个整数,执行程序无法调用一个整数,因为它不是错误消息所提示的“可调用”。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...