问题描述
终止一个 ffmpeg
子进程后,终端被弄乱了 - 打字的字符是不可见的!该输入仍然可以在该命令中执行,但是键盘输入不被回送到终端。
After terminating an ffmpeg
subprocess, the terminal gets messed up - typed characters are invisible! The input still works in that commands can be executed, but keyboard input is not echoed to the terminal.
发出shell命令重置
将所有内容恢复正常(或!reset
),所以一个解决方法是在脚本中调用 os.system('reset')
。
Issuing shell command reset
puts everything back to normal (or !reset
from within ipython), so a workaround the issue is calling os.system('reset')
inside the script.
其他我试过的东西: import curses;在生成子进程之前,curses.initscr()
和终止后的 curses.endwin()
另一个可能相关的问题是,在产生子进程后,交互式终端变得滞后,有时无法捕获类型的字符。
Other things I've tried: import curses; curses.initscr()
before spawning the subprocess and curses.endwin()
after termination, which worked somewhat but broke other stuff. Another possibly related issue is that after spawning the child process, the interactive terminal becomes laggy and sometimes fails to capture typed characters.
产生过程的代码如下所示:
The code to spawn the process looks like:
with open('/tmp/stdout.log', 'w') as o:
with open('/tmp/stderr.log', 'w') as e:
proc = subprocess.Popen([args], stdout=o, stderr=e)
然后停止它:
proc.terminate()
proc.communicate()
这里可能会出现什么问题?
What could be going wrong here?
推荐答案
没有使用 proc.terminate()
。您可以通过
proc.send_signal(signal.SIGINT)
proc.wait()
这允许ffmpeg有机会写任何需要恢复终端的转义序列。
This allows ffmpeg the chance to write whatever escape sequences it needs to restore the terminal.
编辑:稍后发现 - 另一个提示使 ffmpeg
更好地使用 Popen
是为它提供一个 subprocess.PIPE
或在
。否则,它似乎尝试从父级的stdin获取输入,这可能会导致奇怪的终端行为。正在运行的ffmpeg进程正在侦听stdin上的'?'和'q'输入。 stdin
句柄中打开(os.devnull)
edit: discovered later- another tip to make ffmpeg
behave better with Popen
is to provide it a subprocess.PIPE
or open(os.devnull)
in the stdin
handle. Otherwise, it seems to try to get input from the parent's stdin which can cause weird terminal behaviour. A running ffmpeg process is listening for '?' and 'q' input on stdin.
这篇关于端子文本在终止子进程后变为不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!