【发布时间】:2011-09-23 05:10:10
【问题描述】:
在终止ffmpeg 子进程后,终端变得一团糟——输入的字符是不可见的!输入仍然有效,可以执行命令,但键盘输入不会回显到终端。
发出 shell 命令 reset 使一切恢复正常(或在 ipython 中 !reset),因此解决此问题的方法是在脚本中调用 os.system('reset')。
我尝试过的其他事情:在产生子进程之前import curses; curses.initscr() 和在终止之后curses.endwin(),这有点工作但破坏了其他东西。另一个可能相关的问题是,在生成子进程后,交互式终端变得迟钝,有时无法捕获键入的字符。
生成进程的代码如下所示:
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()
这里可能出了什么问题?
【问题讨论】:
-
我猜这和
stdout=o, stderr=e有关 -
django runserver在重新加载时也存在同样的问题。
-
我已将 stdout 和 stderr 重定向到 os.devnull 并且问题仍然存在
-
我认为您重定向了输出,并且在进程终止后它不会返回。
-
某些东西会改变终端设置并且不会恢复它们。如果是您的代码;你可以use a context manager to restore the terminal whether an error occurs or not。如果它是子进程(
ffmpeg),那么您可以尝试更温和地关闭它(如您的回答所尝试的那样),例如,proc = Popen(..., stdin=PIPE); ...; proc.stdin.write("q")/.close()。
标签: python terminal ffmpeg subprocess