【问题标题】:flush stdout inside numba jitted function在 numba jited 函数中刷新标准输出
【发布时间】:2017-10-26 09:48:45
【问题描述】:

显然numba 既不支持sys.stdout.flush 也不支持print("", flush=True)

在 jitted 函数中刷新“打印”的好方法是什么?

【问题讨论】:

标签: python-3.x stdout flush numba


【解决方案1】:

您可以使用objmode() context manager 来使用numbanopython JIT 模式不支持的代码:

import numba

@numba.njit
def f(x):
    x *= 2
    with numba.objmode():
        print(x, flush=True)
    return x + 1

print(f'f(7) = {f(7)}')
# 14
# f(7) = 15

如文档中所述,由于所涉及的开销,这只能在性能关键代码部分之外使用。

注意:我认为这个问题在 2017 年最初提出时不可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2012-09-12
    • 2022-01-25
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多