reactPHP process start blocking timer

685 Views Asked by At

I have here a reactPHP loop with two timer events:

$loop = \React\EventLoop\Factory::create();

$process = new \React\ChildProcess\Process("php wait5seconds.php");

$process->on('exit', function($exitCode, $termSignal) {
    echo "Process Finished".PHP_EOL;
});

$loop->addTimer(2, function($timer) {
    echo "TWO".PHP_EOL;
});

$loop->addTimer(1, function($timer) use ($process) {
    echo "ONE".PHP_EOL;
    $process->start($timer->getLoop());
});

$loop->run();

Without the child-process the timers work fine and I get the out put:

ONE
TWO

But when I start a process in the first timer, the second timer is blocked until the process is finished. I get the output:

ONE
Process Finished
TWO

Isn't supposed to be asynchronous? the process is sleep(5) so I expect:

ONE
TWO
Process Finished

What am I missing?

1

There are 1 best solutions below

2
On

Works for me.

Your issue is probably that php wait5seconds.php results in an error and thus doesn't run for 5 seconds. If you replace it with sleep 5, everything works as expected.