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

140 Views Asked by At

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.

2

There are 2 best solutions below

1
rishi On BEST ANSWER

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/

3
Amogha Hegde On

Yes, when you use Python's print() function, it's usually a blocking thing. Basically, it writes whatever you want to say to the console, and while it's doing that, everything else waits around until it's done