Keep a subprocess alive and keep giving it commands? Python

后端 未结 3 1535
醉话见心
醉话见心 2020-11-28 10:21

If I spawn a new subprocess in python with a given command (let\'s say I start the python interpreter with the python command), how can I send new

3条回答
  •  情歌与酒
    2020-11-28 10:46

    Don't.

    If you want to send commands to a subprocess, create a pty and then fork the subprocess with one end of the pty attached to its STDIN.

    Here is a snippet from some of my code:

    RNULL = open('/dev/null', 'r')
    WNULL = open('/dev/null', 'w')
    
    master, slave = pty.openpty()
    print parsedCmd
    self.subp = Popen(parsedCmd, shell=False, stdin=RNULL,
                          stdout=WNULL, stderr=slave)
    

    In this code, the pty is attached to stderr because it receives error messages rather than sending commands, but the principle is the same.

提交回复
热议问题