Swoole uses task function in PHP-fpm/apache

藏色散人
Release: 2023-04-07 07:30:01
forward
2787 people have browsed it

Swoole uses task function in PHP-fpm/apache

● Create a new RedisServer.php

● The code is as follows

set(array(
    'task_worker_num' => 32,
    'worker_num' => 1,
    'task_enable_coroutine' => true,
    'heartbeat_check_interval' => 5,
    'heartbeat_idle_time' => 10,
));
$server->setHandler('LPUSH', function ($fd, $data) use ($server) {
    $taskId = $server->task($data);
    if ($taskId === false) {
        $server->send($fd, Server::format(Server::ERROR));
    } else {
        $server->send($fd, Server::format(Server::INT, $taskId));
    }
});
$server->on('Finish', function($serv, $taskID, $data) {
    $stats = $serv->stats();
    if ($stats['tasking_num'] > 10) { //tasking_num 当前正在排队的任务数
        echo "剩余任务信息:" . json_encode($serv->stats()) . "\n";
    }
});
$server->on('Task', function ($serv, $data) {
    go(function () {
        usleep(50000);
    });
    var_dump($data);
});
$server->start();
Copy after login

task inside usleep (50000); Simulate task execution time

● Create a new Queue.php

● The code is as follows

connect('127.0.0.1', 9501);
$x=1; 
while($x <= 1000) {
  $redis->lpush("myqueue", json_encode(array("hello".$x, "swoole")));
  $x++;
}
Copy after login

Simulate 1000 task delivery

After testing, it will be processed in 1 second

You can follow Task The speed of task execution is adjusted. task_worker_num controls the number of started processes.


● These processes are managed by the swoole bottom layer. The bottom layer will re-create new tasks after a fatal error occurs or the process exits. Process

task_worker_num


● The maximum value shall not exceed SWOOLE_CPU_NUM * 1000

● The processing time of a single task, such as 100ms, which one The process can process 1/0.1=10 tasks in 1 second

● Task delivery speed, for example, 2000 tasks

● 2000/10=200 are generated per second, you need to set task_worker_num => ; 200, enable 200 task processes

Related recommendations: [PHP Tutorial]

The above is the detailed content of Swoole uses task function in PHP-fpm/apache. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!