Python Forum
temporarily buffer output, and output it later
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
temporarily buffer output, and output it later
#1
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
Reply
#2
start by showing your code, and pointing out where you expect things are going wrong.
Reply
#3
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
Reply
#4
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
Reply
#5
(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
Reply
#6
Output errors to a file, then print out the file when done?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can rich.logging output to file? pyfoo 1 185 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  problem in output of a snippet code akbarza 2 300 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  Reading and storing a line of output from pexpect child eagerissac 1 4,145 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  output shape problem with np.arange alan6690 5 610 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  Error with output djprasanna 1 509 Nov-28-2023, 06:40 PM
Last Post: deanhystad
  [split] Why is there an output of None akbarza 1 416 Nov-27-2023, 02:53 PM
Last Post: deanhystad
  Unexpected output Starter 2 439 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 654 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 469 Oct-24-2023, 05:56 AM
Last Post: buran
  output values change akbarza 3 487 Oct-18-2023, 12:30 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020