0

I am trying to accomplish a pseudo console with python's subprocess module for my web app. The point of the webapp is to be able to run and interact with python code via the web. I have a simple test.py script that has some sleeps and an input for testing purposes.

I am using process = subprocess.Popen(['python', "-u",'test.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) to run the file in a new subprocess.

I am retrieving the output lines like this:

for line in iter(process.stderr.readline, ''):
        #line = line.rstrip('\n')
        print(line)
        sys.stdout.flush()  # Flush the output buffer

Whenever my test.py python program gets to the input line, the print doesn't print the input line itself unless there is a newline character within the input prompt of test.py.

I was wondering if there is any way to get the output of the current line of the subprocess rather than only beeing able to access "processed" lines.

I also tried accessing stderr to no avail, I tried iterating over stdout.read until an empty char or a newline char was found, also to no avail.

Would love any help regarding this!

Furry
  • 13
  • 3
  • You’re… calling `readline`. Is it supposed to return less than a line? – Davis Herring Jul 07 '23 at 20:22
  • @DavisHerring Well no, it is just not the right function for what I am looking for. The documentation on the subprocess module is subpar at best. I am trying to get the current line the subprocess is on before it is processed, as I understand it this isn't accessible at all through stdout. – Furry Jul 08 '23 at 10:59
  • I think the documentation is assuming you know how interprocress communication works (for example, when should an “empty char” appear?). There already are [questions about that](https://stackoverflow.com/q/1410849/8586227), of course. – Davis Herring Jul 08 '23 at 22:08
  • @DavisHerring I am very confused, bear with me pls. I have checked out the question you linked but didn't really get any wiser, since I am already using -u when calling the python subprocess. I guess I don't understand how the subprocess works. Are you able to explain to me where a python input text gets "sent" to before it's beeing processed? If I run the python with the input in a regular cli it prints out the input text without issue, I just don't understand how to access it via the subprocess module – Furry Jul 11 '23 at 12:45
  • I didn't even notice that you were running with `-u`, sorry. (Not all programs have such an option, so I was trying to address the general case.) In that case, I simply [can't reproduce](https://wandbox.org/permlink/39Q0GgAwDLxl9ChZ) your results; maybe you're calling `read()` (which reads until end of **file**, not just line) rather than `read(1)`? – Davis Herring Jul 12 '23 at 06:28
  • @DavisHerring try adding a variable that gets assigned via input to slow.py – Furry Jul 13 '23 at 11:24
  • I’m not sure what that’s supposed to mean, but adding a variable assignment will certainly not change the semantics. It’s true that this example just uses standard input, but that’s not a material difference versus the `subprocess` approach. – Davis Herring Jul 13 '23 at 21:22

0 Answers0