ReactPHP确实是异步的吗? [英] Is ReactPHP truly asynchronous?

查看:149
本文介绍了ReactPHP确实是异步的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在ReactPHP上进行一些测试,因为它看起来很棒.我已经使用以下 react/socket 代码对其进行了测试,以用于简单的套接字服务器.

$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();

直到这一点都没有问题.当客户端连接并且客户端收到响应时,服务器显示New client !.

但是我做了一个新的测试,对data事件进行了更多处理.为了说明我的话,我将添加一个for循环,该循环将花费几毫秒的时间:

$conn->on('data', function ($data) use ($conn) {
    $conn->write("Wow, some data, such cool\n");

    for ($i=0; $i<10000000; $i++); // here

    $conn->close();
});

在这种情况下,如果有10个客户端,则客户端将在所有客户端处理(大约2秒)后显示文本Wow, some data, such cool,但是服务器将显示New client !,而无需等待. >

因此,在这里我缺乏理解,ReactPHP是异步I/O,但是PHP是单线程,如果输入和输出之间存在大量处理,则会阻塞所有客户端

解决方案

ReactPHP是一个异步I/O,但是PHP是单线程的,如果输入和输出之间有很多处理,则会阻塞所有客户端.

是的

ReactPHP的灵感很大程度上来自遵循相同原理的node.js.这种基于事件的模式的目标不是利用服务器的16个CPU,而是通过处理HTTP请求B来充分利用处理器,而对数据库A的请求A的控制器则被暂停,直到数据库请求成功"为止. '事件被调用.

您的测试与node.js和ReactPHP所做的假设完全相反:计算速度快,I/O速度慢",因此,如果我们在I/O期间(而不是在I/O之间)进行计算,则CPU时间总会比需要的时间多.

如果要使用服务器16 CPU,则使用no​​de.js或ReactPHP,只需在16端口上启动16服务器进程,然后在它们前面放置一个负载平衡器(如nginx)即可.

但是请记住,ReactPHP仍处于试验阶段,尚未准备好投入生产.

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.

解决方案

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.

Yes.

ReactPHP is very much inspired by node.js, which follows the same principle. The goal of such event-based patterns is not to exploit your server 16 CPU's, but to exploit fully your processor by processing HTTP request B while your controller for request A, which has made request to database, is paused until the 'database request success' event is called.

Your test is going exactly against the assumption made by node.js and ReactPHP: "computation is fast, I/O are slow", so if we do computation during I/O (and not between I/O), then CPU time will always be available in higher quantity than needed.

With node.js or ReactPHP if you want to use your server 16 CPU, you just launch 16 server process on 16 port and put a load balancer like nginx in front of them.

But keep in mind ReactPHP is still experimental and not ready for production.

这篇关于ReactPHP确实是异步的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆