我正在编写一个python应用程序,用于在unix管道中交互使用。应用程序应该触发一个基于诅咒的终端UI,并且基于用户交互,只在退出之前写入标准输出。
典型的用法是一个典型的管道:
foo_command | my_application | sink_app我遇到的问题是,python诅咒库在应用程序运行时将各种东西发送到stdout。此外,sink_app在运行my_application时开始执行。
sink_app何时开始执行和何时停止接受输入?根据我收集的信息,我需要保存对stdout文件描述符的引用,以便以后可以写入它。并通过另一个fd (哪一个?)敬诅咒。应该是通过newterm(),但这在python诅咒绑定中是不可用的。
发布于 2018-12-10 01:27:47
发布于 2020-05-29 03:09:16
作为suggested,杂耍输入流。这不是很好的测试,但似乎在正确的轨道上。
import os
# back up the piped stdin
mystdin_fd = os.dup(0)
# open the terminal for reading keyboard events
termin = open("/dev/tty")
# replace stdin with the terminal, so curses initscr() and getch() work
os.dup2(termin.fileno(), 0)
# initialise curses - https://docs.python.org/3/howto/curses.html
...
# read from the piped stdin
mystdin = open(mystdin_fd, 'r')
for line in mystdin:
...https://stackoverflow.com/questions/53696818
复制相似问题