Bounty: 50
I will have two jobs in my script. Once a job started other will run async. I used Thread for this. And this thread will return some info while other count that info.
What I want to do is while counter’s value is changing, thread also be continue to run.
Display that I want:
-----------------------------------------
Count: 5
-----------------------------------------
thread keeps running...
thread keeps running...
thread keeps running...
Actually I achive this goal using curses
module, but this is not exactly what I wanted. Because when I press ^C
terminal contents is gone. I want them to freeze in the screen.
Code with curses:
import sys
import time
import queue
import signal
import curses
import threading
def ctrl_c_handler(*args):
sys.exit(0)
signal.signal(signal.SIGINT, ctrl_c_handler)
MESSAGE = "thread keeps running..."
def print_func(message):
return message
def new_window(stdscr):
que = queue.Queue()
curses.curs_set(False)
y, x = stdscr.getmaxyx()
draw = x * "-"
i = 3
count = 1
while True:
thread = threading.Thread(target=lambda q, arg1: q.put(print_func(arg1)), args=(que, MESSAGE,), daemon=True)
thread.start()
result = que.get()
try:
stdscr.addstr(0, 0, draw)
stdscr.addstr(1, 0, f"Count: {str(count)}")
stdscr.addstr(2, 0, draw)
stdscr.addstr(i, 0, result)
except curses.error:
pass
stdscr.refresh()
time.sleep(0.1)
i += 1
count += 1
if i == y:
stdscr.clear()
i = 3
curses.wrapper(new_window)
Is there a way to achive the same goal without using curses, or curses with no loss of contents?
Thank you!