[英]Interactive Python Interpreter Run in Background
I've run into a weird problem when starting an interactive python console in the background. 在后台启动交互式python控制台时,我遇到了一个奇怪的问题。 After resuming the interpreter from the background, it does not display any of the text I type (ie it just shows the >>> prompt, though it will interpret whatever I write. Pressing [enter] created another >>> prompt on the same line).
从后台恢复解释器后,它不会显示我键入的任何文本(即,它只会显示>>>提示符,尽管它将解释我编写的内容。按[enter]在同一位置创建另一个>>>提示符线)。
An easy way to reproduce the problem is just to type: 重现此问题的简单方法是键入:
python &
fg
This problem does does not occur if you start the program in the foreground, put it in the background, and return it to the foreground: 如果您在前台启动程序,将其放在后台,然后将其返回到前台,则不会发生此问题:
python
[ctrl-z]
bg
fg
If you're wondering why you might want to start an interactive interpreter in the background, consider the following scenario: 如果您想知道为什么要在后台启动交互式解释器,请考虑以下情形:
I have a simulation that takes a long time to run, but after it's done, I want to interact with the results. 我的模拟需要很长时间才能运行,但是完成后,我想与结果进行交互。 Thus, I started the program:
因此,我启动了该程序:
python -i simulation.py &
fg #(after it's finished running)
The easy solution is just to start it in the foreground, move it to the background, and then later bring it to the foreground, but I'm just wondering why this happens. 简单的解决方案是将其启动在前台,将其移至后台,然后再将其置于前台,但我只是想知道为什么会这样。
Consider executing the following commands to force the terminal ECHO mode after you have brought your interpreter into the foreground: 在将解释器带入前台后,请考虑执行以下命令以强制终端ECHO模式:
import termios
attr = termios.tcgetattr(1)
attr[3] = attr[3] | termios.ECHO
termios.tcsetattr(1, termios.TCSANOW, attr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.