重用子流程obj

2024-09-30 01:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python脚本,它需要发出许多shell命令。我想我可以创建一个subprocess对象,然后在每次执行命令时重用它。在

我的代码是这样设置的:

def setupPipeline(self):
    setupShell = subprocess.Popen([''], stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

    # Stop running pipeline
    setupShell.stdin.write('stop ' + self.Name)
    output = setupShell.stdout.read()
    print output

    # Cancel any running jobs and cleanup variables
    setupShell.stdin.write('sudo -u pr cancel ALL')
    output = setupShell.stdout.read()
    print output

    setupShell.stdin.write('sudo -u pr clean ALL')
    output = setupShell.stdout.read()
    print output

(此处跳过许多其他代码)

^{pr2}$

但是,当代码到达第二个命令时,我得到一个

IOError: [Errno 32] Broken pipe

如何重用子进程对象来发出需要在同一shell中执行的命令?这些命令不能简单地链接在一起,因为每次调用之间都有处理。在


Tags: 对象代码命令selfreadoutputstdinstdout
1条回答
网友
1楼 · 发布于 2024-09-30 01:24:20

创建执行shell的子进程,并向其发送命令。在shell中运行一个空命令,这会导致shell执行空命令,然后退出。在

shell = subprocess.Popen("/bin/bash -i".split(), stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)

相反,您可以考虑使用^{},因为它听起来好像您正在重新发明它。。在

相关问题 更多 >

    热门问题