0

I'm trying to run two processes in parallel. Both programs do not "end" without Ctrl+C (by the way, I'm on Linux), and so os.system will not return the output of a command. I want a way to create two processes independently of the main Python thread, and read text from them as it appears. I also want to be able to send characters to the process (not as a command, because the process interprets key presses by itself) I need something like this:

process1 = System("sh process1") process2 = System("sh process2") process1.Send("Hello, I'm sending text into process 1.") text = process1.Read() process2.Send(text) 

Is there a way of doing this? I've looked into the Subprocess module, but I'm not sure it achieves quite what I want - or if it does, I'm not sure how to do it.

many thanks to anyone who answers,

1 Answer 1

2

Subprocess does what you want. Here's an example of writing to and reading from an external command:

import subprocess proc = subprocess.Popen(["sed", "-u", "s/foo/bar/g"], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("foobar\n"); print proc.stdout.readline(); # Writes "barbar" 
Sign up to request clarification or add additional context in comments.

2 Comments

I have a problem. When trying to read a line, the program freezes. It should be outputting a Line. Is there any way to avoid this?
You can only read a line if the program outputs a line. Make sure it's outputting a full line, and that it's not buffering the output.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.