[英]How to I make python curses application pipeline friendly?
我正在編寫一個旨在在 unix 管道內交互使用的 python 應用程序。 應用程序應該啟動一個基於curses 的終端用戶界面,並根據用戶交互,僅在退出前寫入標准輸出。
典型用法是典型管道的用法:
foo_command | my_application | sink_app
我遇到的問題是,當應用程序運行時,python curses 庫將各種內容發送到 stdout。 此外, sink_app
在my_application
運行時開始執行。
sink_app
開始執行以及何時停止接受輸入?根據我收集的信息,我需要保存對 stdout 文件描述符的引用,以便以后可以寫入。 並將另一個 fd(哪個?)傳遞給 ncurses。 據說是通過 newterm() 實現的,但這在 python 詛咒綁定上不可用。
按照建議,處理輸入流。 這沒有經過很好的測試,但似乎在正確的軌道上......
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:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.