[video tutorial] the latest version of swoole installation and tasks function test

Time:2020-2-11

Today, let’s install and test PHP’s multi concurrent and high-performance network communication extension. This extension is developed by using c voice. After it is loaded into PHP, it realizes multi concurrent asynchronous communication at the PHP level, simulates many features of go voice, and greatly widens the PHP application scenarios.

You can use the command on the official website directly. When installing swoole, there may be errors and stuck. Try it several times more and you will succeed.
pecl install swoole
To configure the CLI environment of PHP and the INI file of FPM environment, 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->("Content-Type", "text/plain");
            $response->("Hello World\n");
});

$http->start();

Note that there will be some errors when using httpclient. First, the new version of swoole removes swoole \ http \ client. It will be reported that the class cannot be found. Second, execute get method in the process

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

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, "distribute task with task ID of $task" \ n ");
});

$serv->on('Task', function (Swoole\Server $serv, $task_id, $from_id, $data) {
    Echo "data received by the Tasker process";
    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);

 

  

Recommended Today

Understanding cookie, session, token, JWT

History of development 1. A long time ago,WebBasically, it’s just browsing documents. Since it’s browsing, as a server, it doesn’t need to record who browsed what documents in a certain period of time. Each request is a new oneHTTPProtocol is request plus response, especially I don’t have to remember who just sent itHTTPRequests, every request […]