나는 많은 쉘 명령을 발행해야하는 파이썬 스크립트를 가지고있다. 하위 프로세스 객체를 만든 다음 실행 명령이있을 때마다 그 객체를 다시 사용할 수 있다고 생각했습니다.서브 프로세스 오브젝트 재사용
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
그러나
if __name__ == '__main__':
#self-test code
pipelineObj = Pipeline(sys.argv)
pipelineObj.setupPipeline()
(여기에 다른 코드를 많이 건너 뛰기), 코드 내가 얻을 두 번째 명령에 도달 할 때 :
이
은 내가 내 코드가 설정 한 방법입니다IOError: [Errno 32] Broken pipe
동일한 쉘 내에서 모두 실행해야하는 명령을 실행하기 위해 서브 프로세스 객체를 재사용하는 방법은 무엇입니까? 이 명령들은 각 호출 사이에 처리가 진행 중이므로 함께 묶일 수 없습니다.
(가) 왜 반복해서 같은 쉘을 다시 사용해야합니다? (b) 쉘을 사용해야하는 이유는 무엇입니까? 인수를 사용하여 명령을 직접 분리하여 배열을 사용하여 명령을 실행할 수 있습니다. (http://docs.python.org/library/subprocess.html#subprocess.Popen) - 'PATH'도 'IFS' 환경 변수는 프로그램 실행에 영향을줍니다. – sarnold
각 명령의 결과가 후속 명령 쉘 환경에 영향을 주어야하기 때문에. –