pythonblockingnonblocking

Is Python's `print()` function blocking or non-blocking?


I'm trying to speed up the processing time of a script, which in certain configurations may have a lot of output dumped to the console (file=stdout) via print(). Is Python's print() function blocking or non-blocking? I've not able able to find an adequate answer for this in the documentation.

I'm running Linux 4.18.0-486.el8.x86_64 GNU/Linux.


Solution

  • This is the implementation of print() in python which is essentially a write function with various formatting tasks.

    https://github.com/python/cpython/blob/v3.11.2/Python/bltinmodule.c#L1986

    It is possible to turn on non blocking file writes on unix, however, turning on non-blocking mode has no visible effect for regular files

    f = os.open("fname", os.O_CREAT | os.O_WRONLY | os.O_NONBLOCK)
    

    From what I understand write tasks fill a buffer [cached] and are written to disk after.

    Furthermore on POSIX,

    That is, writes must be strongly consistent–that is, a write() is required to block application execution until the system can guarantee that any other read() call will see the data that was just written. https://www.nextplatform.com/2017/09/11/whats-bad-posix-io/