Python persistent popen

Is there a way to make multiple calls in the same “session” in Popen? For example, can I make a call through it, and then another one after it, without linking the commands into one long line?

+3
source share
3 answers

You don't “make a call” when you use popen, you run the executable and talk to it on stdin, stdout and stderr. If the executable has some way of executing a “session” of work (for example, by reading lines from stdin), then yes, you can do it. Otherwise, you will need to execute several times.

subprocess.Popen (basically) is just a wrapper around execvp (3)

+3

, ( ), , - :

from subprocess import *
p = Popen(['/bin/sh'], shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE)

, ,:

>>> p.stdin.write("cat /etc/motd\n")
>>> p.stdout.readline()
'Welcome to dev-linux.mongo.com.\n'

(, stderr, Popen stdout). , stdin stdout , "" . , recipe ActiveState, , .

: /, , select Python, , stdout ( stderr, ), :

>>> select.select([p.stdout], [], [], 0)
([<open file '<fdopen>', mode 'rb' at 0x10341690>], [], [])
+1

, , ?

, shell = True. , . shell = False ( ) /arg.

"" Popen? , , , ?

- Popen ? , .

0

Source: https://habr.com/ru/post/1703864/


All Articles