3

I am running a Python script on a Rasberry Pi which appears to freeze intermittently on the print() call. The script only freezes occasionally and continues when someone focuses on the terminal and presses the "enter" button or hits ctrl+c.

My guess is that the problem here is that the print() call is waiting to get some kind of response back from the terminal which it doesn't get when the user does something unexpected, such as highlight text, right-click drag, or some other thing (which I am unable to replicate). To be clear, the script hangs indefinitely in this case, and not for the 120 seconds specified in the time.sleep() call directly below.

My question is this: is there any print call I can use in python which doesn't wait to get a response back from the console so that it doesn't freeze in unknown cases like the above?

I've put my code below, although the specifics of what it does are not particularly relevant to the question.

while True:
    directory_pump_files =  glob.glob("./pumplog*.csv") #read in some files from the file system

    for file_path in directory_pump_files:
        try_upload_file(file_path, pump_files_done, "/batchUpload/create") #upload the pump file

        
    directory_log_files =  glob.glob("./log*.csv")

    for file_path in directory_log_files:
        try_upload_file(file_path, log_files_done, "/batchUpload/SensorReadings") #upload the log file
    print ("sleep")
    time.sleep(120)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Jack
  • 871
  • 1
  • 9
  • 17
  • 2
    The `print` call is followed by a `time.sleep`. Isn't the sleep that's causing your program to pause and "freeze" for a while? – Gino Mempin Jan 24 '21 at 22:44
  • The program hangs indefinitely, not just for the 120 seconds specified in time.sleep Cheers for clarifying this - I have edited the question to be more understandable – Jack Jan 24 '21 at 22:47
  • See [How to flush output of print function?](https://stackoverflow.com/q/230751/2745495) – Gino Mempin Jan 24 '21 at 23:11
  • 1
    What is consuming stdout? When it hangs indefinitely, has 'sleep' been output? Sounds like buffering - if the output buffer gets full, writing to that buffer gets blocked. Presumably the consumer of the buffer isn't emptying it. Consider making the buffer (a lot) larger - but if the consumer is really sluggish, that will only postpone the situation where the buffer is full, i.e. `print()` will still block. The real problem is the consumer of the buffer. – DisappointedByUnaccountableMod Jan 24 '21 at 23:40
  • The consumer is just the standard rasberry pi terminal program LXTerminal 0.3.2 – Jack Jan 24 '21 at 23:42
  • 1
    When it hangs indefinitely, has 'sleep' been output? – DisappointedByUnaccountableMod Jan 24 '21 at 23:49
  • Indeed it does, although the nature of the loop is that it outputs "sleep" every two minutes and gets a new file from the filesystem every hour. So all I see in the terminal is a notification that the file has uploaded, and then a bunch of sleep statements, and then a hang. So I don't have proof of which statement it's breaking on, as all I can see from the terminal is a bunch of lines saying "sleep." I am just inferring that this is a terminal issue, as ctrl+c seems to clear the script to keep running. https://imgur.com/a/a3JeXOz – Jack Jan 25 '21 at 00:01

1 Answers1

2

It might be an issue with the terminal's buffering mechanism. Normally, the buffer is flushed when a newline is encountered, but things could work differently on a Raspberry Pi.

You can try to use the flush argument. From help(print):

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

By using print("sleep", flush=True) the string will be printed immediately.

Jaap Joris Vens
  • 3,382
  • 2
  • 26
  • 42