Swoole can be widely used in the Internet, mobile communications, enterprise software, cloud computing, online games, Internet of Things (IOT), Internet of Vehicles, smart homes and other fields. Using PHP + Swoole as a network communication framework can greatly improve the efficiency of enterprise IT R&D teams and focus more on developing innovative products. Recently, PHP's asynchronous, parallel, high-performance network communication engine Swoole has released version 1.10.0. This version adds several new features. Not much to say below, let’s take a look at the detailed introduction, I hope it can help everyone.
Automatic DNS resolution
The new version of the asynchronous client no longer needs to use swoole_async_dns_lookup to resolve domain names, and the bottom layer implements automatic domain name resolution. Client can directly pass in the domain name when executing the connect method.
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); $client->on("connect", function(swoole_client $cli) { $cli->send("GET / HTTP/1.1\r\n\r\n"); }); $client->on("receive", function(swoole_client $cli, $data){ echo "Receive: $data"; $cli->send(str_repeat('A', 100)."\n"); sleep(1); }); $client->on("error", function(swoole_client $cli){ echo "error\n"; }); $client->on("close", function(swoole_client $cli){ echo "Connection close\n"; }); //底层会自动进行异步域名解析 $client->connect('www.baidu.com', 9501);
Slow request log
The new version adds the function of tracking slow requests, which can record the PHP function call stack of slow requests.
function test() { test_sleep(); } function test_sleep() { echo "sleep 5\n"; sleep(5); } $server = new swoole_server('127.0.0.1', 9501); $server->set([ 'worker_num' => 1, 'task_worker_num' => 1, 'trace_event_worker' => true, 'request_slowlog_timeout' => 1, 'request_slowlog_file' => '/tmp/trace.log', ]); $server->on('Receive', function($serv, $fd, $reactor_id, $data) { test(); $serv->send($fd, "Swoole: $data"); }); $server->start();
After processing the slow request, a line of error message will be printed in the /tmp/trace.log log:
[08-Jan-2018 15:21:57] [worker#0] pid 26905 [0x00007f60cda22340] sleep() /home/htf/workspace/swoole/examples/server/trace.php:10 [0x00007f60cda222e0] test_sleep() /home/htf/workspace/swoole/examples/server/trace.php:4 [0x00007f60cda22280] test() /home/htf/workspace/swoole/examples/server/trace.php:28 [0x00007f60cda22190] {closure}() /home/htf/workspace/swoole/examples/server/trace.php:42 [0x00007f60cda22140] start() /home/htf/workspace/swoole/examples/server/trace.php:42
New STREAM module
New stream Modules make the communication methods between Reactor, Worker, and Task processes more flexible and decoupled to the greatest extent. Complex online projects use the stream mode to make request allocation and scheduling more efficient.
$serv = new swoole_server("127.0.0.1", 9501); $serv->set(array( 'dispatch_mode' => 7, 'worker_num' => 2, )); $serv->on('receive', function (swoole_server $serv, $fd, $threadId, $data) { var_dump($data); echo "#{$serv->worker_id}>> received length=" . strlen($data) . "\n"; }); $serv->start();
To communicate between Reactor and Worker, use dispatch_mode = 7 to enable
Communication between Worker and Task, use task_ipc_mode = 4 Enable
Add Event::cycle function
User code can customize an EventLoop hook function. This function will be executed at the end of each round of event loop. transfer. It is convenient to use Generator + Yield or Promise-like Swoole framework to implement your own scheduler.
Swoole\Timer::tick(2000, function ($id) { var_dump($id); }); Swoole\Event::cycle(function () { echo "hello [1]\n"; Swoole\Event::cycle(function () { echo "hello [2]\n"; Swoole\Event::cycle(null); }); });
Other updates
Swoole and websocket simple chat room implementation method
php uses swoole to update client data in real time
Using Swoole to asynchronously capture web pages practical sharing
The above is the detailed content of Detailed explanation of the new version of Swoole 1.10.0. For more information, please follow other related articles on the PHP Chinese website!