是否可以在jit函数中包含函数列表?

问题描述 投票:0回答:1

我只是想知道是否可以在jit函数中使用函数列表甚至函数字典。

例如

def func1():
    pass

def func2():
    pass

list_ = [func1, func2]

@njit
def jit_func(list_):
    # do something with list of functions

难以实施这样的事情,所以想知道是否有可能/是否有解决方法。

python python-3.x jit numba
1个回答
0
投票

您可以在基本函数上使用装饰器,并使用lambda或通用f函数来运行函数列表。如果要运行的功能很多,则可以使用Pool.map并行运行这些功能,并将所有输出收集为结果列表。

from multiprocessing import Pool


@njit
def func1():
    return 'func1'

@njit
def func2():
    return 'func2'

def f(fun):
    '''
    Run a function.
    '''
    return fun()

list_ = [func1, func2]

with Pool(2) as p:
    r = p.map(f, list_)
© www.soinside.com 2019 - 2024. All rights reserved.