Home > Backend Development > PHP Tutorial > Let's talk about how PHP allows the Swoole/Pool process pool to implement Redis persistent connections

Let's talk about how PHP allows the Swoole/Pool process pool to implement Redis persistent connections

藏色散人
Release: 2023-04-11 10:26:01
forward
4219 people have browsed it

This article brings you relevant knowledge about php, which mainly introduces how to use PHPphp to enable Swoole/Pool process pool to achieve Redis persistent connection. Friends who are interested can take a look at it together. I hope it will be helpful to everyone. .

Let's talk about how PHP allows the Swoole/Pool process pool to implement Redis persistent connections

php Let Swoole | Pool process pool implement Redis persistent connection

Process pool, based on Swoole\Server's Manager management process module accomplish. It can manage multiple work processes. Compared with Process to implement multiple processes, Process\Pool is simpler and has a higher encapsulation level. Developers can implement process management functions without writing too much code. With Co\Server, pure coroutine style can be created. , a server-side program that can utilize multi-core CPUs.

Swoole process pool implements redis data reading

In the following case, the Redis process pool is started through WorkerStart and the Redis list data is read persistently; when WorkerStop disconnects all connections Recycle all child processes.

Step one: Encoding code

File: d10.php

<?php

use Swoole\Process;
use Swoole\Coroutine;

// 指定5个工作进程
$pool = new Process\Pool(5);
// 设置启用协程
$pool->set([&#39;enable_coroutine&#39; => true]);

/**
* onWorkerStart 子进程启动
* @param \Swoole\Process\Pool $pool Pool对象
* @param int $workerId   WorkerId当前工作进程的编号,底层会对子进程进行标号
**/
$pool->on("WorkerStart", function (Process\Pool $pool, $workerId) {
// 输出当前工作进程
   echo "Worker #{$workerId} is started\n";

   // 实例化化连接redis
   $redis = new Redis();
   $redis->pconnect(&#39;127.0.0.1&#39;, 6379);
   // 指定redis键
   $key = "key1";

   // 循环读取列表数据
   while (true) {
    // 弹出列表最后一个元素
       $msgs = $redis->brpop($key, 2);
       // 元素值为空则跳过
       if ( $msgs == null) {
           continue;
       }
       // 打印获取的值
       var_dump($msgs);
       echo "Processed by Worker#{$workerId}\n";
   }
});

// 子进程结束
$pool->on("WorkerStop", function ($pool, $workerId) {
   echo "Worker#{$workerId} is stopped\n";
});

// 启动工作进程
$pool->start();
Copy after login

Step two: Start the Redis service and write list data through the client

This case requires PHP to install the redis extension

# 通过redis客户端连接
./redis-cli

127.0.0.1:6379> lpush key1 &#39;world&#39;
Copy after login

Copy code

Step 3: Run d10.php

php d10.php
Copy after login

Step 4: View the process

ps aux | grep php
root        938  0.0  1.2 129164 12412 ?        Ss   Apr21   0:00 php-fpm: master process (/usr/local/php-8.0.1/etc/php-fpm.conf)
www         951  0.0  0.6 129164  6636 ?        S    Apr21   0:00 php-fpm: pool www
www         952  0.0  0.6 129164  6640 ?        S    Apr21   0:00 php-fpm: pool www
root      12327  0.0  1.2 126992 12800 pts/2    S+   00:02   0:00 php d10.php
root      12328  0.0  0.7 131096  7444 pts/2    S+   00:02   0:00 php d10.php
root      12329  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12330  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12331  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12332  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12355  0.0  0.0 112812   976 pts/3    R+   00:09   0:00 grep --color=auto php
Copy after login

Step 5: Output the result

php d10.php

Worker #1 is started
Worker #2 is started
Worker #3 is started
Worker #4 is started
Worker #0 is started
array(2) {
 [0]=>
 string(4) "key1"
 [1]=>
 string(5) "world"
}
Processed by Worker#1
Copy after login

d10.php file will always be blocked after running and keep reading the redis list data. Once the Redis list is output, it will be immediately popped up and printed on the screen.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Let's talk about how PHP allows the Swoole/Pool process pool to implement Redis persistent connections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:ziruchu.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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template