The question is very related to Consume only N messages from RabbitMQ with react\stomp, ack them separately and then exit, but a bit more general.
For example, I have a slow I/O operation:
$port = 4000;
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) use ($loop){
$conn->on('data', function ($data) use ($conn, $loop) {
if ($data == 42) {
// this instantly stop the loop
$loop->stop();
}
$process = new React\ChildProcess\Process('sleep 5; echo ' . $data);
$loop->addTimer(0.001, function($timer) use ($process, $conn) {
$process->start($timer->getLoop());
$process->stdout->on('data', function($output) use ($conn) {
if ($output) {
$conn->write("> $output");
}
});
});
});
});
echo "Socket server listening on port $port.\n";
echo "You can connect to it by running: telnet localhost $port\n";
$socket->listen($port);
$loop->run();
echo "exited";
when I run $loop->run();
I want to stop it at some point, e.g. by timer, after accepting N requests, or any other event, like pcntl_signal
, or data assertion, as in the example.
The challenge is to finish all started jobs before exit, and I cannot figure out how to achieve it.
In the server console I have:
Socket server listening on port 4000.
You can connect to it by running: telnet localhost 4000
exited
In the client console I have:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
1
2
3
4
5
> 1
> 2
42
Connection closed by foreign host.
where 1,2,3,4,5 were entered with 1 sec interval
Instead I would like to see something like this:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
1
2
3
4
5
> 1
> 2
42
> 3
6
> 4
> 5
Connection closed by foreign host.
$loop
, and then simply call$loop->stop()
, I believe – Okwu$loop->stop()
stops the loop. I have updated the question to make it a bit more clear, that I need the loop to finish all started jobs before exit the script. – Sphygmo