简体   繁体   English

在后台运行的交互式Python解释器

[英]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.

相关问题 是否有可能让Python Interactive Interpreter在加载时运行脚本? - Is it possible to get the Python Interactive Interpreter to run a script on load? Python对象名称和交互式解释器 - Python object names and the interactive interpreter 当我们打开一个交互式python3解释器并运行一些命令时,是否在某个模块中运行这些命令? - When we open an interactive python3 interpreter and run some commands, do we run the commands in some module? asyncio 事件循环可以在后台运行而不暂停 Python 解释器吗? - Can an asyncio event loop run in the background without suspending the Python interpreter? Python Unicode字符串和Python交互式解释器 - Python Unicode strings and the Python interactive interpreter Atom编辑器中的Python交互式shell /解释器 - Python interactive shell / interpreter in the Atom editor 了解Python交互式解释器中的打印字节 - Understanding printing bytes in the Python interactive interpreter 规范的嵌入式交互式 Python 解释器示例? - Canonical embedded interactive Python interpreter example? 复制粘贴到 Python 交互式解释器和缩进中 - Copy-paste into Python interactive interpreter and indentation 如何在解释器中运行python文件 - how to run a python file in interpreter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM