duancashi1362 2019-05-23 21:07
浏览 85

Symfony 3.4处理多个输入问题

I have read the documentation and tried several examples, but can't seem to get the symfony process component to allow me to answer multiple questions as they arise. I know the answers before hand and would love to just feed them in as a string.

The command I'm wanting to run in a process is something like this:

$ introduce
$ > What is your name? 
$ Michael
$ > How old are you?
$ 36
$ > Are you having a good day?
$ Yes
$ Doing lots of stuff now.
$ Info recorded. Thank you!

A stupidly simple example. The command asks three questions and expects an answer to each question. Then (my real command) begins a long process.

I have tried variations of

$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);

$inputs = "Michael
36
Yes
";
$process->setInput($inputs);
$process->enableOutput();

$process->start();

and

$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);

$inputs = new InputStream();
$inputs->write("Michael");
$inputs->write("36");
$inputs->write("Yes");

$process->setInput($inputs);
$process->enableOutput();

$process->start();
$inputs->close()

and

$process = new Process('introduce', MY_DIR);
$process->setTimeout(null);

$inputs = new InputStream();
$process->setInput($inputs);
$process->enableOutput();
$process->start();

$inputs->write("Michael");
$inputs->write("36");
$inputs->write("Yes");
$inputs->close();

And then, to wait for it to finish:

$running = true;
while($running) {
    if (!$process->isRunning()) {
        if (!$process->isSuccessful()) {
            throw new Exception($process->getErrorOutput());
        }

        $running = false;
    }

I know I can use ->wait(), but in my real application, I'm starting multiple instances in parallel and can't let one block before the others finish. So, I add processes to an array and unset them as they finish. When the array is empty, I break out of the loops and move on with the program.

So, what am I doing wrong? How can I have a predetermined input sent to a process so it can answer successive questions and then let the process continue?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试