subprocess.Popen을 사용하여 4 개의 명령을 실행하는 코드는 다음과 같습니다. 아래 코드를 사용하여 로그 파일을 처리하고 있습니다. 아래 코드를 사용하여 파일을 순차적으로 처리하면 정상적으로 작동합니다. 이제는 병렬 처리를 위해 각 파일에 하나씩 쓰레드를 생성하고 각 쓰레드에 대한 함수 아래에 바인딩했습니다. 하지만 그들 중 일부는 나에게 원하는 출력과 일부 던져 오류를 제공합니다.하위 프로세스의 출력 캡처 스레드 함수 내에서 열기
코드 :
def process_log_file(file):
proc= subprocess.Popen(['python27', 'countmapper.py',"C:\\pythonPrograms\\04-03-2014\\17IL\\"+file],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = proc.communicate()
sortedop= subprocess.Popen(['sort'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE,stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = sortedop.communicate(out)
countReducer= subprocess.Popen(['python27', 'countreducer.py'],cwd="C:\pythonPrograms\\",stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = countReducer.communicate(out)
countpostprocesser= subprocess.Popen(['python27', 'countpostprocesser.py'],cwd="C:\pythonPrograms\\",stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, err = countpostprocesser.communicate(out)
jsondata2=json.loads(out)
fd=open(file+".json","w")
json.dump(jsondata2,fd,sort_keys=True,indent=2)
fd.close()
return
오류받은 :
이for file in glob.glob("SAMPLE*.log"):
thread1 = threading.Thread(target=process_log_file,args=(str(file),))
threads.append(thread1)
thread1.start()
# Wait for all threads to complete
for t in threads:
t.join()
사람이 나를 도울 수 :
Exception in thread Thread-42:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\pythonPrograms\counts_batch_threading.py", line 45, in process_log_file
jsondata2=json.loads(out)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
코드는 스레드 생성에 사용?
스레드를 만드는 방식을 보여줄 수 있습니까? 첫 번째 출력물을 읽으려고 할 때 아직 준비가되지 않았습니까? – mdurant
각 단계에서 '출력'값은 얼마입니까? 그것은 당신이 기대하는 것입니까? – Dunes
@mdurant가 요청한 코드를 추가했습니다. – Codelearner