Why PHP programmers should learn to use Swoole

The last two months has been studying Swoole, the research results will be formally released at 6.21 open source, this time there is no runoff article, taking advantage of the holiday to the water of it today.

With this article, I want to be able to Swoole Amway to more people. While Swoole currently positioning may be some senior phper toys, so that the lower daunting, some scenarios might be forced to look ignorant, but actually not so difficult.

In the self-introduction Swoole official website is "PHP engine for asynchronous communication network production environment", the first Swoole it is a web application development tool that supports Http, TCP, UDP, WebSocket.

Swoole and our traditional PHP developers difference is there, there is also a need to understand the concept. At present, some use-based framework developed Swoole, then, from the development of the customs and traditions of TP, LV framework similar.

So why use Swoole?

Yu-run think the following points:

  • Permanent memory, to avoid repeated loading performance cost, enhance the performance Massive

  • Coroutine asynchronous improve I / O intensive concurrent processing scenarios (such as: the development of micro-channel, payment, login, etc.)

  • Easily develop Http, WebSocket, TCP, UDP and other applications, can communicate with the hardware

  • PHP services high-performance micro-architecture to become a reality

Permanent memory

At present the traditional PHP framework before processing each request, have to do it again load the framework document, operational configuration. This could have become a major cause of performance problems, and the use Swoole is not the problem, once loaded multiple times.

Coroutine

As shown below, this scene is the same thread processing concurrent requests, such as an interface you need to call other api interface or read and write large files, traditional synchronous blocking and asynchronous coroutine advantage is reflected out.

Detailed Swoole coroutine Why for I / O intensive scene

Speaking coroutine, you have to briefly talk about the process and thread, the process is very well-known resource intensive, in order to process a request to create a large number of processes is certainly worth the candle. The multi-threaded applications is more and more, there are several levels in the CPU core will perform several tasks, threads, created more than once, there will be loss of thread scheduling.

Coroutine is achieved on the basis of a single thread, it can maximize the use of CPU resources, and not wasted while waiting for I / O. Of course, the number of co-drive take up more memory the more, but this is acceptable, compared to processes and threads, occupied resource is relatively small.

When using coroutine encountered reading and writing files, the request interface scenes, automatically suspend coroutine, the CPU coroutine give other tasks, which can improve utilization of CPU resources of a single thread, reduce waste, improve performance .

Coroutine Code Example:

<?php
use Swoole\Coroutine as co;
 
// 协程
$time = microtime(true);
// 创建10个协程
for($i = 0; $i < 10; ++$i)
{
    // 创建协程
    go(function() use($i){
        co::sleep(1.0); // 模拟请求接口、读写文件等I/O
        echo $i, PHP_EOL;
    });
}
swoole_event_wait();
echo 'co time:', microtime(true) - $time, ' s', PHP_EOL;
 
// 同步
$time = microtime(true);
// 创建10个协程
for($i = 0; $i < 10; ++$i)
{
    sleep(1); // 模拟请求接口、读写文件等I/O
    echo $i, PHP_EOL;
}
echo 'sync time:', microtime(true) - $time, ' s', PHP_EOL;

operation result:

0
9
8
7
6
5
4
3
2
1
co time:1.0087130069733 s
0
1
2
3
4
5
6
7
8
9
sync time:10.010055065155 s

As can be seen from the above results, coroutine execution is not sequential manner, higher performance, when sleep will perform the task at the right thread to the other coroutine.

Creating Http Service

In fact, not difficult to imagine, look at the code:

$http = new swoole_http_server("127.0.0.1", 9501);
$http->on('request', function ($request, $response) {
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();

Micro Services

Tars Tencent from 2008 to today has been unified application framework TAF (Total Application Framework) the logic behind layers used, currently supports C ++, Java, PHP, Nodejs language. The framework provides users with a complete solution involves the development, operation and maintenance, and testing of a product or service to help the rapid development, deployment, testing, on-line. It combines scalable protocol codec, high performance RPC communications framework, the name of the route and found release monitoring, log statistics, configuration management is one that can quickly build your own stable and reliable distributed applications through its use of a micro-services, and achieve a complete and effective service governance.

See: https://segmentfault.com/a/1190000011825769

If wrong welcome that, I really would like to recommend Swoole!

Reproduced in: https: //my.oschina.net/yurun/blog/1831238

Guess you like

Origin blog.csdn.net/weixin_34384915/article/details/91725914