JaeYoung Hwang
JaeYoung Hwang

Reputation: 9

How to continually communicate with same process with Python Popen()

I am new at this so hope you understand...

Now I am using python subprocess module for sending the specific command.

After I send the command1 with Popen function, I would like to send a command2 to same process one more.

Is it possible...?

// Example
// command1 -> Opening command for text program.
// command2 -> Executing the write and save command.(on above text program) 

popen = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
// next....?

please let me know If I understand it incorrectly.

Upvotes: 0

Views: 1460

Answers (3)

Hacker6914
Hacker6914

Reputation: 257

For catching stdout in realtime from subprocess better check catching stdout in realtime from subprocess

Upvotes: 0

tripleee
tripleee

Reputation: 189749

It is perfectly doable but you might want to look into something like pexpect which offers more control over the dialog.

cmd = subprocess.Popen(['command1'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  # no shell=True here!
stdout, stderr = cmd.communicate('command2')

This presumes that command1 will keep running and reading additional commands on its standard input.

Upvotes: 1

Dinesh Pundkar
Dinesh Pundkar

Reputation: 4196

Check this out. In unix, we can execute multiple commands in one go like

cmd_1;cmd_2

We will use same thing with sub-process.

Make sure to use shell=True.

import subprocess
all_cmd = "echo a;echo b;echo c"

p = subprocess.Popen(all_cmd,stdout=subprocess.PIPE, shell=True)
p_stdout,p_err = p.communicate()
print p_stdout.strip()

Upvotes: 0

Related Questions