I've been doing some tests on ReactPHP because it looks pretty awesome. I've tested it with the following react/socket code, for a simple socket server.
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
echo 'New client !';
$conn->on('data', function ($data) use ($conn) {
$conn->write("Wow, some data, such cool\n");
$conn->close();
});
});
$socket->listen(1337);
$loop->run();
Until this point there's no problem. The server shows New client !
when a client is connected and the client receives the response.
But I done a new test, with more processing on the data
event. To illustrate my words, I'll add a for
loop that will take a few milliseconds to complete :
$conn->on('data', function ($data) use ($conn) {
$conn->write("Wow, some data, such cool\n");
for ($i=0; $i<10000000; $i++); // here
$conn->close();
});
In this case, with 10 clients, the client will show the text Wow, some data, such cool
after all clients processing (so ~2 seconds), but server will show New client !
without waiting.
So here my lack of understanding, ReactPHP is an asynchronous I/O, but PHP is single-threaded, and if there is a lot of processing between input and output, that will block all clients.
stream_socket_server
as far as I can see. The question is whetherstream_socket_server
is asynchronous. Given PHP's basic architecture that would surprise me, but I do not know the definite answer. – Twum