ReactPHP是忠实地异步? [英] ReactPHP is truely asynchronous?

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

问题描述

我做的ReactPHP一些测试,因为它看起来像pretty真棒,和我一起反应/插座测试,一个简单的socket服务器。

I doing some tests on ReactPHP because it looks like pretty awesome, and I test with the code of react/socket, 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 here, 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();
});

在这种情况下,10个客户端,客户端会显示文本哇,一些数据,比如酷所有客户端处理后的(SO〜2秒) ,但服务器将显示新的客户端!无需等待。

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.

所以在这里我缺乏了解,ReactPHP是一个异步I / O,但是PHP是的单线程,如果有大量的输入和输出之间的处理,这将阻止所有客户端

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是一个异步I / O,但是PHP是单线程的,如果有大量的输入和输出之间的处理,这将阻止所有客户。

"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是非常受Node.js的,它遵循同样的原则启发。这种基于事件的模式的目标不是利用你的服务器16的CPU,而是通过处理HTTP请求B,而你的请求控制器A已提出请求,数据库被暂停,直到'数据库请求成功'事件是充分利用您的处理器调用。

ReactPHP is very much inspired by node.js, which follow 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.

您的测试是要精确地反对node.is和Reactphp的假设:计算速度快,I / O是缓慢的,所以如果我们在我做计算/ O(而不是我之间/ O),然后CPU时间总是会提供高于所需量。

Your test is going exactly against the assumption made by node.is 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.

使用Node.js的或ReactPHP,如果你想使用你的服务器16个CPU,你只有16端口上推出16个服务器进程,并把负载均衡器像在他们面前nginx的。

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.

但请记住ReactPHP仍处于实验阶段,并没有准备好进行生产。

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

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

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