如何运行 2 多次处理和交换input/output?

How to run 2 process and exchange input/output between them multiple times?

我正在尝试做这样的事情:

Future<String> getOutAndAnswer(testcase) async {
  Process python = await Process.start('python', ['tasks/histogram/run.py']);

  Process java = await Process.start('java', ['solutions/Histogram.java']);

  String results = "";

  for(int i = 0; i < testcase; i++){
    final String out = await python.stdout.transform(utf8.decoder).first;

    java.stdin.writeln(out);

    final String answer = await java.stdout.transform(utf8.decoder).first;

    python.stdin.writeln(answer);

    results += "($out, $answer)";
  }

  return results;
}

基本上,python程序负责生成每个测试用例的输入,然后java程序将接受输入和return答案,然后发送到python 程序来检查它是否正确,等等每个测试用例。

但是当我尝试使用上面的代码时,我收到一条错误消息,提示我已经听过一次流:

Exception has occurred.
StateError (Bad state: Stream has already been listened to.)

Python 程序:

import os

CASE_DIR = os.path.join(os.path.dirname(__file__), "cases")

test_cases = next(os.walk(CASE_DIR))[2]

print(len(test_cases))
for case in sorted(test_cases):
    with open(os.path.join(CASE_DIR, case), 'r') as f:
        print(f.readline(), end='', flush=True)
        f.readline()
        
        expected_output = f.readline()

        user_output = input()
        
        if expected_output != user_output:
            raise ValueError("Wrong answer!")

print("EXIT", flush=True)

Java 程序:

public class Histogram {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            String input = scanner.nextLine();

            String answer = calculateAnswer(input);

            System.out.println(answer);
        }
    }
}

您的问题出在 .first 上,它将收听流,获取第一个元素,然后立即停止收听流。请参阅此处的文档:https://api.dart.dev/stable/2.17.3/dart-async/Stream/first.html

您应该听一次并定义一个 onData 方法来执行这些步骤。请在此处查看 .listen() 的文档:https://api.dart.dev/stable/2.17.3/dart-async/Stream/listen.html

您可以尝试将标准输出流包装在 StreamIterator<String> 中。您将不得不尝试验证一下,但我认为这就是您要找的。

Future<String> getOutAndAnswer(int testcase) async {
  Process python = await Process.start('python', ['tasks/histogram/run.py']);
  Process java = await Process.start('java', ['solutions/Histogram.java']);

  String results = "";

  StreamIterator<String> pythonIterator = StreamIterator(
      python.stdout.transform(utf8.decoder).transform(LineSplitter()));
  StreamIterator<String> javaIterator = StreamIterator(
      java.stdout.transform(utf8.decoder).transform(LineSplitter()));

  for (int i = 0; i < testcase; i++) {
    if (await pythonIterator.moveNext()) {
      final String out = pythonIterator.current;
      if (out == 'EXIT') {
        break;
      }

      java.stdin.writeln(out);

      if (await javaIterator.moveNext()) {
        final String answer = javaIterator.current;

        python.stdin.writeln(answer);

        results += "($out, $answer)";
      }
    }
  }

  await pythonIterator.cancel();
  await javaIterator.cancel();
  return results;
}

您可能需要添加以下导入:

import 'dart:async';
import 'dart:convert';