0

I have a master thread that spawns other threads. The master thread does periodic status checks that I print/flush/update with:

status = "Queued: {} | Workers: {}".format(queued_status, worker_status)
sys.stdout.flush()
sys.stdout.write("\r{}".format(status))

This works great, but it ultimately gets jumbled in with the print statements from the worker threads.

So, how would I designate the status print to the top (it just updates in place), and have the threads print below? Something like this:

Queued: No | Workers: 4
........................
Thread print
Thread print
Thread print

Where the thread print will just keep scrolling (like a normal terminal window), but the status is locked to the top.

martineau
  • 119,623
  • 25
  • 170
  • 301
M Leonard
  • 581
  • 1
  • 7
  • 23
  • 1
    If you're using Linux (or otherwise using a VT100 compatible terminal, or at least one that supports ANSI control sequences), this is pretty easy. Take a look at https://stackoverflow.com/a/45164619/4014959 – PM 2Ring Aug 07 '17 at 18:08

1 Answers1

2

The curses module is the usual solution for printing to a particular place on the screen. You'd still need to arrange for all of the text to be printed from a single thread, to avoid near-simultaneous prints from getting jumbled together; this is typically done with a Queue.Queue (workers add to the queue rather than printing directly; main thread pops from queue if non-empty and prints).

jasonharper
  • 9,450
  • 2
  • 18
  • 42