我有一个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中执行的命令?这些命令不能简单地链接在一起,因为每次调用之间都有处理。在
创建执行shell的子进程,并向其发送命令。在shell中运行一个空命令,这会导致shell执行空命令,然后退出。在
相反,您可以考虑使用^{} ,因为它听起来好像您正在重新发明它。。在
相关问题 更多 >
编程相关推荐