많은 데이터를 뒤섞기위한 스크립트를 작성 중입니다. 문서 내가 (주석 참조) outproc
를 기다릴 때, 비록 내가 교착 상태를 치는거야, 경고로Python : 교착 상태없이 데이터를 스트림하기 위해 서브 프로세스 사용?
outproc = None
for input in input_files:
p = Popen('process_input "%s" | more_input_processing' %(input,),
shell=True, stdout=PIPE)
for line in p.stdout.xreadlines():
if linecount % 1000000 == 0:
outfile = "output%03d" %(linecount // 1000000,)
if outproc:
outproc.stdin.close()
result = outproc.wait() # <-- deadlock here
assert result == 0, "outproc exited with %s" %(result,)
outproc = Popen('handle_output "%s"' %(outfile,),
shell=True, stdin=PIPE)
linecount += 1
outproc.stdin.write(line)
p.stdout.close()
result = p.wait()
assert result == 0, "p exited with %s" %(result,)
: 그것은이 같은입니다.
설명서에서 제안한 "솔루션"은 .communicate()
을 사용하는 것입니다.하지만 이렇게하면 플러시하기 전에 모든 입력을 메모리로 읽어들이는 것이 바람직하지 않습니다.
그래서 어떻게 교착 상태없이 하위 프로세스간에 데이터를 스트리밍 할 수 있습니까?
그래, 실제로 하위 프로세스를 기다리지 않으면 (즉,'.wait()'에 대한 모든 호출을 제거하면 모든 것이 제대로 작동하는 것처럼 보입니다.이 스크립트는 괜찮습니다.). 그래도 제대로 작동하는 방법을 알아낼 수 있다면 좋을 것입니다 ... –