【问题标题】:Is it possible to have list of functions in a jit function?jit 函数中是否可以包含函数列表?
【发布时间】:2020-02-22 12:05:27
【问题描述】:

我只是想知道是否可以在 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】:

    您可以在基本函数上使用装饰器,并使用 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_)
    
    
    >>>r
    ['func1', 'func2']
    

    【讨论】:

    • 谢谢,但我的目标不是 jit 初始函数,更多的是可以访问并能够在 jit 函数中迭代初始函数。
    猜你喜欢
    • 2021-08-16
    • 1970-01-01
    • 2020-01-21
    • 2012-10-10
    • 2018-01-31
    • 2014-01-20
    • 2021-09-19
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多