2013-10-09 46 views
0

我正在尝试编写一个脚本来发送文本并从给定的.exe文件获取输出。 .exe文件向其输出发送脚本将发送到其输入的内容。 发送输入和读取输出应该使用不同的线程完成。发送输入并使用不同的线程获取输出到exe文件

import subprocess 
proc=subprocess.Popen(['file.exe'],stderr=subprocess.STDOUT, stdout=subprocess.PIPE, stdin=subprocess.PIPE) 

stdout, stdin = proc.communicate() 
proc.stdin.write(text) 
proc.stdin.close() 
result=proc.stdout.read() 
print result 

现在我找不到使用单独线程进行通信的方式。

任何指导或帮助表示赞赏。

回答

0

也许你可以尝试这样的事情。您在主线程中发送输入,并在另一个输出中获取输出。

class Exe(threading.Thread): 
def __init__(self, text=""): 
    self.text = text 
    self.stdout = None 
    self.stderr = None 
    threading.Thread.__init__(self) 

def run(self): 
    p = subprocess.Popen(['file.exe'],stdout=subprocess.PIPE,stdin=subprocess.PIPE) 
    self.stdout, self.stderr = p.communicate(self.text) 

text = "input" 
exe = Exe(text) 
exe.start() 
exe.join() 
print exe.stdout 
return 0 
相关问题