Python Forum

Full Version: temporarily buffer output, and output it later
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a program where I have created a monitor thread to tell me that things are still happening while lengthy tasks are running in my main thread. But messages keep appearing which are corrupting the information put out by the monitor thread. What I think I need to do is to stop anything (including error messages) producing output to stdout or to stderr while the monitor thread is running. Preferably I would buffer any such output, and output it once the monitor thread has ceased. How do I do this?

Thanks - Rowan
start by showing your code, and pointing out where you expect things are going wrong.
Here is some relevant code:

messages = Queue()
sleeptime = 0.15

def waiting(message_queue, text, timeout):
    sys.stdout.write(text + "   ")
    sys.stdout.flush()
    i = 0
    while True:
        sys.stdout.write("\b" + whizzer[i % len(whizzer)])
        sys.stdout.flush()
        time.sleep(sleeptime)
        i = i + 1
        if i * sleeptime >= timeout:
            print()
            print("Timeout. Task took longer than " + str(timeout) + " seconds.")
            break
        if message_queue.empty():
            continue
        else:
            flag = message_queue.get(block = False)
        if flag is _finished:
            sys.stdout.write("\r")
            sys.stdout.flush()
            print ()
            print("Task is complete. Took " + str(i * sleeptime) + " seconds")
            # for i in range(len(text) + 3):
            #     sys.stdout.write(" ")
            #     sys.stdout.flush()
            break
        if flag is _interrupted:
            break
    wait = Thread(target = waiting, args = (messages, "Waiting for browser to open and web page to download", 20))
    wait.start()
    driver.get('https://www.opinionstage.com/registrations/login')
    messages.put(_finished)
Here is a typical piece of output to stdout (and/or stderr) that I would like to tidy up:

Processing exam number 0 (topic: air-law)
Waiting for image to upload  —[10516:14800:0304/160133.041:ERROR:device_event_log_impl.cc(211)] [16:01:33.041] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.
\
Task is complete. Took 59.4 seconds
Processing question no. 0
The monitor thread outputs "Waiting for image to upload —", then the main thread (I suppose) outputs "[10516:14800:0304/160133.041:ERROR:device_event_log_impl.cc(211)] [16:01:33.041] Bluetooth: bluetooth_adapter_winrt.cc:1072 Getting Default Adapter failed.". Then the monitor thread outputs lots of -\|/ sequences until the task is complete. I would like to delay the error message until the monitor thread has stopped outputing. How do I do this?

Thanks - Rowan
https://www.cyberciti.biz/faq/how-to-red...r-in-bash/

What about just redirecting the errors so they're not displayed?
python myfile.py 2> /dev/null
(Mar-24-2021, 04:41 PM)nilamo Wrote: [ -> ]https://www.cyberciti.biz/faq/how-to-red...r-in-bash/

What about just redirecting the errors so they're not displayed?
python myfile.py 2> /dev/null

Well, I suppose that would tidy up the output, but it means that I would never see the error output. And it does not deal with the situation where the main thread outputs some messages to stdout while the Waiting thread is outputting its messages. What I would like to be able to do is to redirect all output from the main thread and any error messages to a buffer while the Waiting thread is running, and then to output them once the waiting thread has finished. Is this possible?

Thanks - Rowan
Output errors to a file, then print out the file when done?