I have a GCE app consisting of a single Python script that has some long running functions (most of these are querying databases and sending the results somewhere). It seems that when the script hangs on one of these longer running tasks that nothing is printed to Stackdriver Logging, even print()
statements that come before the place the script is hanging. This seems like bug in Compute Engine or Stackdriver and makes debugging scripts very difficult (e.g. I can't see where the last successful print
statement occurred).
I'd prefer this bug to just be fixed instead of having to add the logging
module as it seems there's a good amount of overhead to set that up.
Per this answer from unix.stackexchange.com, when a process's output is redirected to something other than a terminal, the output may be temporarily stored in a buffer by the operating system. Buffering output increases efficiency by reducing the number of system calls and IO operations.
Buffered output can be flushed manually from within a python script or application.
flush
flag on the print
function.
print('foo', flush=True)
sys.stdout
after printing.
print 'foo'; sys.stdout.flush()