【问题标题】:Send input and get output using different threads to an exe file使用不同的线程向 exe 文件发送输入和获取输出
【发布时间】:2013-10-17 02:18:07
【问题描述】:

我正在尝试编写一个脚本来发送文本并从给定的.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

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

感谢任何指导或帮助。

【问题讨论】:

    标签: python multithreading subprocess pipe


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多