python交互式子进程通信

时间:2017-10-15 00:36:55

标签: python subprocess

我正在尝试学习如何编写交互式子进程通信。

我需要不断读取stdout并写入stdin,下面是我的代码,它有点"工作"但我不确定我是否正在写(它是非常黑客的代码)

假设我有一个名为app.py的脚本,如下所示

import logging
import random

def app():
    number1 = random.randint(1,100)
    number2 = random.randint(200,500)
    logging.info("number1: %s, number2: %s", number1, number2)
    ans = input("enter sum of {} and {}: ".format(number1, number2))
    logging.info("received answer: %s", ans)
    try:
        if int(ans) != number1+number2:
            raise ValueError
        logging.info("{} is the correct answer".format(ans))
    except (ValueError,TypeError):
        logging.info("{} is incorrect answer".format(ans))

def main():
    logging.basicConfig(level=logging.DEBUG, filename='log.log')
    for x in range(10):
        app()

if __name__ == '__main__':
    main()

与上述脚本(app.py)进行交互我有一些非常难看的代码

import queue
import time
import threading
import subprocess
import os
import pty
import re

class ReadStdout(object):
    def __init__(self):
        self.queue = queue.Queue()
        self._buffer_ = []

    def timer(self, timeout=0.1):
        buffer_size = 0
        while True:
            if len(self._buffer_) > buffer_size:
                buffer_size = len(self._buffer_)
            time.sleep(timeout)
            if len(self._buffer_) == buffer_size and buffer_size!=0:
                self.queue.put(''.join(self._buffer_))
                self._buffer_ = []
                buffer_size = 0

    def read(self, fd):
        while True:
            self._buffer_.append(fd.read(1))

    def run(self):
        timer = threading.Thread(target=self.timer)
        timer.start()
        master, slave = pty.openpty()
        p = subprocess.Popen(['python', 'app.py'], stdout=slave, stderr=slave, stdin=subprocess.PIPE, close_fds=True)
        stdout = os.fdopen(master)
        read_thread = threading.Thread(target=self.read, args=(stdout,))
        read_thread.start()
        while True:
            if self.queue.empty():
                time.sleep(0.1)
                continue
            msg = self.queue.get()
            digits = (re.findall('(\d+)', msg))
            ans = (int(digits[0])+int(digits[1]))
            print("got message: {} result: {}".format(msg, ans))
            p.stdin.write(b"%d\n" %ans)
            p.stdin.flush()

if __name__ == '__main__':
    x = ReadStdout()
    x.run()

我觉得我没有采用正确的方式。什么是与另一个脚本交互的正确方法(我需要stdout,而不是盲目写入stdin)

由于

1 个答案:

答案 0 :(得分:0)

此代码适用于您的app.py,因此您可以从中获得交互的基本逻辑。另外,我建议你研究一下pexpect模块。在任何情况下 - 你必须知道对运行程序的期望以及它的输入行是如何结束的。或者你可以在读取行时实现一些超时,因此如果出现不符合预期的情况,就有可能引发异常。

import subprocess
from functools import partial

child = subprocess.Popen(['app.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)

# iterate while child is not terminated
while child.poll() is None:
    line = ''
    # read stdout character by character until a colon appears
    for c in iter(partial(child.stdout.read, 1), ''):
        if c == ':':
            break
        line += c
    if "enter sum" in line:
        numbers = filter(str.isdigit, line.split())
        numbers = list(map(int, numbers))
        child.stdin.write("{0}\n".format(sum(numbers)))