Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive Python Interpreter Run in Background

Tags:

python

linux

bash

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 (i.e. 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.

like image 599
sheridp Avatar asked Nov 02 '22 11:11

sheridp


1 Answers

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)
like image 57
Alexander L. Belikoff Avatar answered Nov 09 '22 11:11

Alexander L. Belikoff