终止子进程后终端文本变得不可见 [英] Terminal text becomes invisible after terminating subprocess

查看:32
本文介绍了终止子进程后终端文本变得不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在终止一个 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 使一切恢复正常(或 ipython 中的 !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() 不被使用.您可以使用

Change the script so that proc.terminate() is not used. You can stop an ffmpeg subprocess more politely with

  proc.send_signal(signal.SIGINT)
  proc.wait()

这让 ffmpeg 有机会编写恢复终端所需的任何转义序列.

This allows ffmpeg the chance to write whatever escape sequences it needs to restore the terminal.

edit: 稍后发现 - 使 ffmpegPopen 一起表现更好的另一个技巧是为其提供一个 subprocess.PIPEopen(os.devnull)stdin 句柄中.否则,它似乎会尝试从父级的标准输入获取输入,这可能会导致奇怪的终端行为.正在运行的 ffmpeg 进程正在监听?"和标准输入上的q"输入.

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.

这篇关于终止子进程后终端文本变得不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆