Installation of the latest version of swoole and functional testing of TASKS

Keywords: PHP network

Today we will install and test the multi-concurrent and high-performance network communication extension of php. This extension is developed by using C voice. After loading into PHP, it realizes multi-concurrent and asynchronous communication at the level of PHP. It simulates many characteristics of go voice and greatly broadens the application scenario of PHP.

You can use the command on the official website directly. There may be errors and stuck when you install swoole. You can succeed by trying several times.
pecl install swoole
To configure ini files for php cli and fpm environments, load the extended so.

Video address:

https://www.bilibili.com/video/av70354024/

httpServer.php

$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
        echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
        $response->header("Content-Type", "text/plain");
            $response->end("Hello World\n");
});

$http->start();

Note that there are some errors when using httpClient. First, the new version of swoole removes Swoole Http Client, the report class cannot be found, and then the get method is executed in the collaboration.

httpClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
	    $cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
	    echo "received: {$data}\n";
});
$client->on("error", function($cli){
	    echo "connect failed\n";
});
$client->on("close", function($cli){
	    echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

taskServer.php

<?php
$serv = new Swoole\Server("127.0.0.1", 9502, SWOOLE_BASE);

$serv->set(array(
    'worker_num' => 2,
    'task_worker_num' => 4,
));

$serv->on('Receive', function(Swoole\Server $serv, $fd, $from_id, $data) {
    echo "receive data" . $data . "\n";
    $data = trim($data);
    $task_id = $serv->task($data, 0); 
    $serv->send($fd, "Distribution of tasks id by $task_id\n");
});

$serv->on('Task', function (Swoole\Server $serv, $task_id, $from_id, $data) {
    echo "Tasker Processes receive data";
    echo "#{$serv->worker_id}\tonTask: [PID={$serv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".PHP_EOL;
    $serv->finish($data);
});

$serv->on('Finish', function (Swoole\Server $serv, $task_id, $data) {
    echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
});

$serv->on('workerStart', function($serv, $worker_id) {
    global $argv;
    if($worker_id >= $serv->setting['worker_num']) {
        swoole_set_process_name("php {$argv[0]}: task_worker");
    } else {
        swoole_set_process_name("php {$argv[0]}: worker");
    }
});

$serv->start();

  

taskClient.php

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
        $cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
        echo "received: {$data}\n";
});
$client->on("error", function($cli){
        echo "connect failed\n";
});
$client->on("close", function($cli){
        echo "connection close\n";
});
$client->connect("127.0.0.1", 9502, 0.5);

Posted by karassik on Mon, 07 Oct 2019 19:12:30 -0700