私はマルチスレッドのためのクラスを実装しようとしており、出力を使って自分のプログラムで次に何をするかを決定したいと思っています。 self.processの出力を文字列として返すにはどうすればよいですか? self.process.communicateの出力を返そうとすると、エラーが発生します。サブプロセスの出力をクラス内から返す
#class for multi threading
class Command(object):
def __init__(self,cmd):
self.cmd = cmd
self.process = None
def run(self,timeout):
def target():
print("Thread started")
self.process = subprocess.Popen(self.cmd,stdout=subprocess.PIPE)
self.process.communicate()
print("Thread finished")
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("\nTerminating process")
self.process.terminate()
thread.join()
print(self.process.returncode)
def unzip_file(zipped):
command = Command(zip_exe+' x '+zipped)
command.run(timeout = 12000)
unzip_file(zipped)
subprocess.communicate()はタプル(stdoutdata、stderrdata)を返します。それがプロセスの出力です。 –
「unzip_file」を実行した後、どうすればアクセスできますか? – sparrow