终止子进程后终端文本变得不可见

编程入门 行业动态 更新时间:2024-10-25 15:24:37
本文介绍了终止子进程后终端文本变得不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在终止一个 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() procmunicate()

这里可能出了什么问题?

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: 稍后发现 - 使 ffmpeg 与 Popen 一起表现更好的另一个技巧是为其提供一个 subprocess.PIPE 或 open(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.

更多推荐

终止子进程后终端文本变得不可见

本文发布于:2023-11-08 06:53:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:终端   进程   文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!