Python 不使用Communication或pexpect的交互式非阻塞subprocess.Popen脚本

Python 不使用Communication或pexpect的交互式非阻塞subprocess.Popen脚本,python,subprocess,popen,Python,Subprocess,Popen,A:为什么会堵塞 B:我如何轻轻按摩,使它不会堵塞 #!/usr/bin/env python import subprocess as sp import os kwds = dict( stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, cwd=os.path.abspath(os.getcwd()), shell=True, executable='/bin/bash', bufsize=1

A:为什么会堵塞

B:我如何轻轻按摩,使它不会堵塞

#!/usr/bin/env python
import subprocess as sp
import os

kwds = dict(
    stdin=sp.PIPE,
    stdout=sp.PIPE,
    stderr=sp.PIPE,
    cwd=os.path.abspath(os.getcwd()),
    shell=True,
    executable='/bin/bash',
    bufsize=1,
    universal_newlines=True,
)
cmd = '/bin/bash'
proc = sp.Popen(cmd, **kwds)
proc.stdin.write('ls -lashtr\n')
proc.stdin.flush()

# This blocks and never returns
proc.stdout.read()
我需要这个以交互方式运行

这是一个简化的示例,但实际情况是我有一个长时间运行的过程,我想启动一个shell脚本,它或多或少可以运行任意代码,因为它是一个安装脚本

编辑: 我希望在几个不同的登录上有效地获取.bash_历史,将其清理为单个脚本,然后在Python脚本中存储的shell中逐行执行新编制的shell脚本

例如:

> ... ssh to remote aws system ...
> sudo su -
> apt-get install stuff
> su - $USERNAME
> ... create and enter a docker snapshot ...
> ... install packages, update configurations
> ... install new services, update service configurations ...
> ... drop out of snapshot ...
> ... commit the snapshot ...
> ... remove the snapshot ...
> ... update services ...
> ... restart services ...
> ... drop into a tmux within the new docker ...
这需要人工数小时;它应该是自动化的

A:为什么会堵塞

它阻塞是因为.read就是这样做的:它读取所有字节,直到文件结束指示。由于进程从不表示文件结束,因此.read永远不会返回

B:我如何轻轻按摩这个部位,使它不会阻塞

要做的一件事是使进程指示文件结束。一个小的改变是导致子进程退出

proc.stdin.write('ls -lashtr; exit\n')

这是我的另一个答案的一个例子:,它没有使用pexpect。你可以在答案中看到更多细节

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import select
import termios
import tty
import pty
from subprocess import Popen

command = 'bash'
# command = 'docker run -it --rm centos /bin/bash'.split()

# save original tty setting then set it to raw mode
old_tty = termios.tcgetattr(sys.stdin)
tty.setraw(sys.stdin.fileno())

# open pseudo-terminal to interact with subprocess
master_fd, slave_fd = pty.openpty()

# use os.setsid() make it run in a new process group, or bash job control will not be enabled
p = Popen(command,
          preexec_fn=os.setsid,
          stdin=slave_fd,
          stdout=slave_fd,
          stderr=slave_fd,
          universal_newlines=True)

while p.poll() is None:
    r, w, e = select.select([sys.stdin, master_fd], [], [])
    if sys.stdin in r:
        d = os.read(sys.stdin.fileno(), 10240)
        os.write(master_fd, d)
    elif master_fd in r:
        o = os.read(master_fd, 10240)
        if o:
            os.write(sys.stdout.fileno(), o)

# restore tty settings back
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)

您的示例代码可以替换为check_output['ls','-lashtr']与问题不符。它过于简单,对于文本中描述的问题没有意义。使用子流程与子流程进行基于对话框的交互并不容易。在一般情况下,略去考虑一下。问题而不是代码的内容太宽泛了:什么能分离出错误。如何找到多个命令输出之间的边界?什么是重新运行?我想要一个开放的进程并保持它开放。沟通无法帮助我,因为它关闭了流程。如果你需要更多的信息,考虑一下:DOKER Run-I-T Ubuntu/bin /BASH;苏;运行脚本;检查输出;不要告诉我们你认为什么不起作用:1你可能是错的,例如,你可以使用。通信运行多个命令。2它没有告诉我们你的实际问题是什么。回答我之前评论中的问题。我不确定我是否能说得更清楚:我想要一个持久化的shell,可以在网络上运行任意shell命令,特别是因为我处理的是docker容器。我想要一个简单的例子来解释这个问题。如果你有更好的解释方法,我很乐意编辑。我关闭了文件描述符。我没有看到我所问问题的答案。你应该帮助别人来帮助你。我想保持沉默。