我一直在使用 numba 进行多处理。
唯一的问题 - numba 分别为每个进程重新编译代码。
(当进程数量等于物理 CPU 数量时,这并不是什么大问题,但如果不是这种情况,那就是一个巨大的问题了!)
有没有办法让 numba 编译一次代码,然后跨进程边界共享编译后的工件?
例子 -
from multiprocessing import Process
from time import time, sleep
from numba import njit
@njit
def child():
pass
if __name__ == "__main__":
ps = [Process(target=child) for _ in range(100)]
for p in ps:
p.start()
s = time()
for p in ps:
p.join()
print("compile time:", time() - s)
compile time: 19.10037922859192
所有核心的 CPU 使用率均固定为 100%。我已经尝试过numba的cache=True,但不幸的是我的代码无法缓存。
/Users/dev/PycharmProjects/trading/tradingdo/strategy.py:91: NumbaWarning: Cannot cache compiled function "_strategy1" as it uses dynamic globals (such as ctypes pointers and large global arrays)
@njit
素胚勾勒不出你
相关分类