简体   繁体   中英

Interactive Python Interpreter Run in Background

I've run into a weird problem when starting an interactive python console in the background. 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).

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:

import termios
attr = termios.tcgetattr(1)
attr[3] = attr[3] | termios.ECHO
termios.tcsetattr(1, termios.TCSANOW, attr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM