如果有一个使用curses库输出的Python应用程序,就像@Rawing建议的那样:https://docs.python.org/3.5/howto/curses.html。它是预先构建好的并且随手可得。
如果有更多的应用程序需要输出数据,有几种方法可以实现。首先,您可以使用byobu或类似工具,在同一屏幕上拥有来自不同应用程序的输出的分割终端。其次,您可以有一个广播应用程序,该应用程序从工作应用程序(或线程)收集数据,并稍后使用curses显示它们(参见上文)。
此外,您可以将数据转储到文件中,然后使用Linux watch命令定期显示内容:
watch cat file
还有很多其他的方法。
import win32console
import multiprocessing
def subprocess(queue):
win32console.FreeConsole() #Frees subprocess from using main console
win32console.AllocConsole() #Creates new console and all input and output of subprocess goes to this new console
while True:
print(queue.get())
#prints any output produced by main script passed to subprocess using queue
if __name__ == "__main__":
queue = multiprocessing.Queue()
multiprocessing.Process(target=subprocess, args=[queue]).start()
while True:
print("Hello World in main console")
queue.put("Hello work in sub process console")
#sends above string to subprocess and it prints it into its console
#and whatever else you want to do in ur main process
你也可以使用线程来实现这个功能。如果你想要队列功能,就必须使用队列模块,因为线程模块本身不带队列。
curses
(用于在终端上分割屏幕并生成自己的用户界面)之外,您还可以看一下progressbar,它提供了所需的功能。 - albert