在我的脚本中,我将有两份工作。一旦作业启动,其他作业将异步运行。我用线做这个。这个线程将返回一些信息,而其他线程将计算这些信息
我想做的是,当计数器的值改变时,线程也会继续运行
显示我想要的:
-----------------------------------------
Count: 5
-----------------------------------------
thread keeps running...
thread keeps running...
thread keeps running...
实际上,我使用curses
模块实现了这个目标,但这并不是我想要的。因为当我按下^C
时,终端内容消失了。我希望他们在屏幕上冻结
带诅咒的代码:
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)
有没有一种方法可以实现同样的目标而不使用诅咒,或者诅咒内容不会丢失
谢谢大家!
试试这个:
curses.wrapper
就是这样做的。按如下方式更改ctrl_c_handler
函数:并不像使用
wrapper
函数那样启动new_window
:相关问题 更多 >
编程相关推荐