11

Apparently numba supports neither sys.stdout.flush nor print("", flush=True).

What is a good way to flush "prints" inside a jitted function?

nivniv
  • 3,421
  • 5
  • 33
  • 40

1 Answers1

5

You can use the objmode() context manager to use code which is not supported in numba’s nopython JIT mode:

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

As noted in the documentation, this should only be used outside of performance-critical code sections due to the overhead involved.

Note: I don’t think this was available when the question was originally asked in 2017.

Socob
  • 1,189
  • 1
  • 12
  • 26